• Post Reply Bookmark Topic Watch Topic
  • New Topic

Find three digits numbers whose digits have (sum >= 10)  RSS feed

 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I need to write a program that is supposed to find three digits numbers from interval, then find if the sum of those numbers is higher or equal to 10. The code is:



The problem I encountered, and don't know how to deal with, is - while in my while loop ( ^^ ), the variable i gets to zero, and because of that can't go back to int loop, to get i++ and go through the whole thing all over again. ( I explained much of it in comments).
I tried to assign variable i to another variable, but did me no good. Any ideas how I should do it ? Or maybe I should write the code in a different way?

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i'm reading your code right, you are using I to iterate through the nubmers 100 - 999. So only use it for that. If you need to then pick the number apart and do stuff with it, then assign i to a temp variable inside the loop and pick THAT apart - leaving i untouched...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:The code is:

Akimbas,

Please DontWriteLongLines. It makes your thread very hard to read.
I'd break yours up, but you have tons of them; and for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

I suggest you use the 'Edit' button to correct your post.

Other than that: follow Fred's advice.

Thanks.

Winston
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first I tried to assign i variable to another variable in while loop, that was my mistake - when I tried to do it right after for loop started, it worked.There was additional problem - the sum of digits - it did not restart by by the start of the next loop, so I fixed it by writing sum = 0; at the end of for loop. Thanks, somehow I did not think that I could put a variable there.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:There was additional problem - the sum of digits - it did not restart by by the start of the next loop, so I fixed it by writing sum = 0; at the end of for loop.

You could just declare sum inside the for loop. It has no meaning outside of that loop anyway.
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Akimbas Akimbasas wrote:There was additional problem - the sum of digits - it did not restart by by the start of the next loop, so I fixed it by writing sum = 0; at the end of for loop.

You could just declare sum inside the for loop. It has no meaning outside of that loop anyway.

I didn't mean that I put it outside of for, I meant that I had put it at the end. If I would have put sum outside, then the sum would not get to zero, and when the next loop would start, the sum would add itself to the previous sum.
I rewrote the code a bit since it didn't actually do what it was meant to. The code was meant to calculate every number and write only those that met the conditions - three digits number that has sum of digits => 10. What I did was I only let the program calculate from range 100 to 999. The new version seems to work, so I won't write it here.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:
Pawel Pawlowicz wrote:
Akimbas Akimbasas wrote:There was additional problem - the sum of digits - it did not restart by by the start of the next loop, so I fixed it by writing sum = 0; at the end of for loop.

You could just declare sum inside the for loop. It has no meaning outside of that loop anyway.

I didn't mean that I put it outside of for

I didn't mean it either... I meant you should declare it inside of the loop.

Like:


Instead of

 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is how I did it, it's still inside for loop, is it not I guess you're right, even if it is at the start or at the end, it does not make any difference, but at start I wrote it at while loop. By the way I would like to ask something. When the program looks for answers, if there are bunch of them, it repeats the same line over and over again with the different number. How should I write the code to make it write the line once, and the answers(numbers) numerous times?
Like let's say there are 3 answers, so:
Three digit numbers, that have sum of their digits higher => 10: i1, i2, i3
or
Three digit numbers, that have sum of their digits higher => 10:
i1
i2
i3


 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:This is how I did it, it's still inside for loop, is it not

The declaration isn't, no. You should always declare variables in the smallest possible scope. The sum variable is only used in the for loop, so it should be declared local to the for loop - not visible to the whole method. When your code gets more complex, it makes it a lot easier to see what's going on.
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Akimbas Akimbasas wrote:This is how I did it, it's still inside for loop, is it not

The declaration isn't, no. You should always declare variables in the smallest possible scope. The sum variable is only used in the for loop, so it should be declared local to the for loop - not visible to the whole method. When your code gets more complex, it makes it a lot easier to see what's going on.

I see, I'll keep that in mind, thanks.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote: . . . makes it a lot easier to see what's going on.
So would correct indentationI copied and pasted the code onto Eclipse and removed some of the long and empty lines. This is what it ought to look like:-Note how you break the long String literal.
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Note how you break the long String literal.


I see, thanks for the feedback. I read that page about the style of writing code, what got on my mind is indentation. "All indenting is done with spaces, not tabs. All indents are four spaces." Does this mean that it is recommended to push "space" four times instead of pushing tab once ? Sounds not like something people would be glad to do every time a new line needs to be written.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akimbas Akimbasas wrote:I read that page about the style of writing code, what got on my mind is indentation. "All indenting is done with spaces, not tabs. All indents are four spaces." Does this mean that it is recommended to push "space" four times instead of pushing tab once ? Sounds not like something people would be glad to do every time a new line needs to be written.

Of course not! Every decent IDE has an option co "to convert" hitting tab key into putting four spaces into source code file.
Notepad ++ (recommended for Java beginners) can do the same if properly configured (or at least it should, never used that functionality of N++ myself).
 
Akimbas Akimbasas
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Of course not! Every decent IDE has an option co "to convert" hitting tab key into putting four spaces into source code file.
Notepad ++ (recommended for Java beginners) can do the same if properly configured (or at least it should, never used that functionality of N++ myself).


I see, I figured as much that programmers would do something about that. )
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, NotePad++ will do that. You set an automatic indentation option and convert tab to 4 spaces.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!