Forums Register Login

Loops

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Think about a little loop that says

and a bigger loop that says

Can you see how to fit those together?
+Pie Number of slices to send: Send
you can put another while block after
goOn = EasyIn.getChar(); to evaluate user input
+Pie Number of slices to send: Send
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.
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1074 times.
Similar Threads
prog of multiple inputs
Logical Error :/
method help
For loop Pyramid Program
catch statements
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:53:54.