• Post Reply Bookmark Topic Watch Topic
  • New Topic

S = 3 + 33 + 333 + ... + 33 ... 3.  RSS feed

 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write a program that does this -> Sum = 3 + 33 + 333 + ... + 33 ... 3. math. I need user to be able to choose how many times does the user want to continue this progression, for example if I he chooses 3 times, then the computattion goes like this: 3 + 33 + 333 <- 3 times


Yeah it doesn't work :/ I seem to get the idea how it should be calculated, with a calculator at least, but I am having hard time to come up with an algorithm, that does computations based on how many times the user wants for it to be done. Any tips ?









 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get you started I have corrected the problems that were preventing compilation. you should compare this with your previous code to understand what was problematic, from a missing semicolon to having no main method. Now you can start working on your algorithm. Start by writing down the result you want to see if the user enters 0, the result for entering 1, the result for entering 2, etc.
I also advise to print out the intermediate results in each iteration of the loop; I don't think they're working as they should.
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BlueJ seemed to compile the code I written code without problems. Thanks, maybe I should try to play with debugger more and I think I will be try to choose lower number, I always picked 5, and it kinda gets to a big number.
 
Vass Raptop
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

I'm sure you'll have this figured out by now, but just for completion's sake:

Compiling issues aside (as pointed out by Ulf), the problem does lie within your while loop:

First of all, for a=1 your code would produce y=x + (x+x*10), i.e. y=3+33 (the loop runs once), whereas for a=1 you want y=3. Secondly, x retains its value of 3 everytime the loop runs, therefore for i.e. a=2, y = 3 + (3 + 3*100) = 3 +303.

I've come up with the following code:

[Moderator action: full code solution deleted]
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vass Raptop wrote:Hi there.

Vass,

  • 1. Welcome to JavaRanch.
  • 2. Please read the DontBeACodeMill (←click) page. We like to help people to their own solutions here, rather than hand them ones - especially in the 'Beginning Java' section. I'm sure your motives were great, but we prefer not to spoon-feed here.
  • 3. The great danger of giving out complete solutions is that they might be WRONG, and I'm afraid yours was, which is why I've deleted it (or rather - ONE of the reasons why). Specifically, it does NOT do what Akimbas asked for.

  • Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akimbas Akimbasas wrote:I seem to get the idea how it should be calculated, with a calculator at least, but I am having hard time to come up with an algorithm, that does computations based on how many times the user wants for it to be done. Any tips ?

    Yes: StopCoding (←click). And read the page thoroughly.

    If you think you know how to do it with a calculator, then write down ALL those steps, in detail and in your own language, not in Java. And test them on paper.

    When, and only when, you know you have a working description: turn your computer back on and translate it into Java code.

    Code does not solve problems; you do.

    Vass has pointed out one problem with your code, but there are others: specifically, you're only doing half the job. See if you can work out what I mean. If you can't, come back and I'll explain.

    Winston
     
    Vass Raptop
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Winston and thanks for deleting the code as well as posting out that it was wrong (I'm kind of embarassed now). This is my first attempt at Java (and programming in general), so I have corrected a few errors in it and I will follow this thread to see if I got it right myself.

    Just for clarification, is it just the result you want the program to display or am I missing something more in what you;re asking? i.e. for a=1 you need it to display "Sum: 3", for a=2 -> "Sum: 36" etc? Or do you want something more?

     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vass Raptop wrote:Hello Winston and thanks for deleting the code as well as posting out that it was wrong (I'm kind of embarassed now). This is my first attempt at Java (and programming in general), so I have corrected a few errors in it and I will follow this thread to see if I got it right myself.

    No probs. It's a common mistake. And notice that I left in your advice, which is spot on.

    Just for clarification, is it just the result you want the program to display or am I missing something more in what you;re asking? i.e. for a=1 you need it to display "Sum: 3", for a=2 -> "Sum: 36" etc? Or do you want something more?

    I think that's a question for Akimbas rather than me. All I can tell you is that your code didn't do what was asked - and neither does his, BTW - because they're missing out an important part of the requirement - at least as stated in the title.

    Winston
     
    Akimbas Akimbasas
    Ranch Hand
    Posts: 32
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Managed to work it out. Thank you for your answers, everyone, it is encouraging just to only get replies. There was one mistake in my code, I missed a semicolon, sorry, but apart from that it compiles without problems.
    Vass Raptop" wrote:Just for clarification, is it just the result you want the program to display or am I missing something more in what you;re asking? i.e. for a=1 you need it to display "Sum: 3", for a=2 -> "Sum: 36" etc? Or do you want something more?

    Yes, the console is required to only show the answer - sum.
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's good to hear.

    ...but apart from that it compiles without problems.

    Maybe BlueJ knows how to run code that doesn't have a main method -it is made for beginners, after all- but if you want to run code outside of an IDE you will definitely need a main method. Just something to keep in mind.
     
    Campbell Ritchie
    Marshal
    Posts: 56593
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    BlueJ has its own main method, it would appear. You can click any method, supply it with a suitable set of arguments, and execute that method. So yes, you can run code without a main method.

    I do not like BlueJ at all. But that is only my opinion.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!