• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop help  RSS feed

 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have created a loop in my program it loops a question ten times, which is using if statements.

An example is

Program: Person 1 do you have a car??
User : Yes
Program: Good

<Loop>

Program: Person 2 do you have a car??
User : No
Program: That's OK!


<Loop>

Now my problem is that when somebody answers with something other than "Yes" or "No" The else kicks in and says "Sorry please answer with "Yes" or "No" only!"
But then this counts as one towards my loop. So what I want is the program to minus one from the loop. I achieved this by using the -- on the loop but the else statement will only allow one thing. So I only get "Sorry please answer with "Yes" or "No" only!" or else I delete that and use my --. How can I get both??

Thanks very much for reading. I am very new to programming!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show your code. It's impossible to say what you're doing wrong in your code without actually seeing it.
 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Please show your code. It's impossible to say what you're doing wrong in your code without actually seeing it.


Thanks for the reply, Sorry I know this is really annoying but this is for a school project and I fear if I post the code I may get into trouble.

Is there anything I can do?

Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write an equivalent example that demonstrates your problem but isn't tightly tied to your assignment?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a random guess...you are probably using the equals() method against a literal like "Yes". you may want to 'normalize' the input - i.e. make it all uppercase, and then compare to "YES". Or check the String API and see if there is some way to see if Strings are equal but ignore the case of the letters.

As for your loop...again it is a guess, but assuming you have a loop counter, if the user inputs an invalid response, just do a "i--" on it when you print the "Yes or no only" line.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Collins wrote:but the else statement will only allow one thing

Only if you haven't put your statements between {}. Your if statement should look something like this
 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Karl Collins wrote:but the else statement will only allow one thing

Only if you haven't put your statements between {}. Your if statement should look something like this


This was it!!!

YES!!! THANKS SOOO MUCH TO EVERYBODY!!! And especially Joanne!!! Epic!!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Collins wrote:
Joanne Neal wrote:Only if you haven't put your statements between {}.


This was it!!!

YES!!! THANKS SOOO MUCH TO EVERYBODY!!! And especially Joanne!!! Epic!!!

So you have learned an important lesson today. ALWAYS use brackets, even if you only have one line in your block. eventually you will try putting in another line and forget they are not there, and your code will break.

ALWAYS ALWAYS ALWAYS use them. They take less than a second to put in, don't cost you anything, and will save you HOURS of debugging.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!