• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bad sentinel loop structure for intro to java class  RSS feed

 
Aaron Zamecnik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have constructed a basic program that adds up the number of entries for numbers in groups of ten (for instance, if you enter 33 it goes in the 31 - 40 section.) It is stored in arrays and i am trying to make a while sentinel loop that will keep the program going. However, it fails to keep running or never ends. Can anybody help?

Code:

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Aaron Zamecnik,

Welcome to CodeRanch!

Its good to see that you've used code tags in your very first post. And you've also posted a complete code which does not contain any compilation error

However, please TellTheDetails instead of simply mentioning that 'it doesn't work'.

Now coming to your problem, I could see some flaws in code:
1) You are asking user to enter 1 if it wants to continue (user will say 'no' to 'are you done' if it wants to continue). But your while condition is : 'done != 1'. This means, if user wants to continue, it is actually giving wrong input to code (or rather, code is expecting wrong input). The question should be - 'do you want to continue? 0 for yes and 1 for no' (to me, 0 for yes and 1 for no looks a little weird, but that can be fixed laterone )
2) After the while loop, you've put an interesting condition in for loop. You are initializing i with 1, and then comparing it with 11 (i == 11) which will never be true, and program will never enter in for loop.

Apart from that, I think these 9-10 if conditions are redundant. This work can be simply done by taking modulus (% operator).

I hope this helps.
 
Aaron Zamecnik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! It now works well, i removed the 1 for yes and 0 for no. I decided a string was a much better way.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should consider some arithmetic on the “county” number. You realise that the index you are incrementing is the same as county / 10, so in this special instance you can dispense with all the if-elses?
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I meant to say /10 itself for county. By mistake, I mentioned % operator
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Zamecnik wrote:Thank you! It now works well, i removed the 1 for yes and 0 for no. I decided a string was a much better way.

You are welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!