• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loops

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic