Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loops

 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a loop that if the user types in the char "y" then the program is repeated and if the user types in anything else then the program is ended.
I want to make it so that when the user types in only the char "n" the program ends and if the user types in anything apart from "y or n" then the program will ask the user to enter "y or N"
my code example:
//declare variables
char goOn = 'y';
while (goOn == 'Y'|goOn == 'y')
{
System.out.println ("Hello");

System.out.println("To re run the program enter Y. Or N to quit");
goOn = EasyIn.getChar();
}
//Write out end message
System.out.println("\n\nEnd of Program\n\n");
}

(ps.i am new to programming)
[ November 26, 2003: Message edited by: macca Mason ]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Macca
Welcome to the ranch. A couple of things, first and most importantly, you never asked a question. Does your code compile? Does it run? do you get any compilation errors? Or an any runtime exceptions? Or does it run fine and not behave as you expect it to?
The first thing that jumps out is your while statement:
while (goOn == 'Y'|goOn == 'y')
The | is not the one you're looking for. You most likely want the ||, for the logical or. The single | is the bit operator 'or'.
Also, in the future, when you post code use the UBB code tags to make it easier to read.
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave.
Thx for replying.
Yeah my code runs fine, all I want it to do is ask the user at the end of the program if they wish to re-run the program or quit. By entering Y or N.
If the user enters anything apart from Y or N then I want the program to say "please enter Y or N". At the moment my program just quits when the user enters any character apart from Y. (when the user enters Y however the programs re-runs).
How can I make it so just Y and N are accepted characters in my while statement.

My full code is.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about a little loop that says

and a bigger loop that says

Can you see how to fit those together?
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can put another while block after
goOn = EasyIn.getChar(); to evaluate user input
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chi and Stan for the help.
I will try to put this in my code. (but finding where about to put it will be difficult)
Thanks alot. If I have any more questions I'll post again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic