Results 1 to 5 of 5
Thread: Using while loop in Java?
- 06-25-2011, 05:01 PM #1
Using while loop in Java?
Hi,
I am new to the Java language and I'm still clueless on some stuff. I guess I'll be around this website for some time.
I'm writing my very first program in Java.
I did a while loop with some if statements within. My loop has four different options. Every option does something different, and option 4 is designed to end the program.
The problem is that I don't know how to make the loop repeat itself until I decide to end it (by choosing option #4)?
Since I put 4 different while(s) loops, and so the programs shuts itself after I input four different options. (independently of what options I punch in)
This is my code.
import java.util.Scanner; //Needed for Scanner class
public class proyectoquefunciona
{
public static void main(String[] args)
{
String cname, caddress, cemail;
int option1, option2, option3, option4;
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to THE STORE");
System.out.println("Please Select One of Following");
System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
option1 = keyboard.nextInt();
{
while (option1 != 0)
{
if (option1 ==1)
{ System.out.println("Good job you're in part (1)");}
if (option1 ==2)
{System.out.println("Good job you're in part 1.2");}
if (option1 ==3)
{System.out.println("Good job you're in part 1.3"); }
if (option1==4)
{System.exit(0);}
break;
}}
System.out.println("Please Select One of Following");
System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
option2 = keyboard.nextInt();
{
while (option2 !=1 || option2 != 2 || option2 !=3 || option2 !=4)
{ if (option2 ==1)
{
System.out.println("Good job you're in part (2)");}
if (option2 ==2)
{System.out.println("Good job you're in part 2.2"); }
if (option2 ==3)
{System.out.println("Good job you're in part "); }
if (option2==4)
{System.exit(0);}
break; }}
System.out.println("Please Select One of Following");
System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
option3 = keyboard.nextInt();
{
while (option3 !=1 || option3 != 2 || option3 !=3 || option3 !=4)
{ if (option3 ==1)
{
System.out.println("Good job you're in part (33)");}
if (option3 ==2)
{System.out.println("Good job you're in part 33.2"); }
if (option3 ==3)
{System.out.println("Good job you're in part 33.3"); }
if (option3==4)
{System.exit(0);}
break; }}
System.out.println("Please Select One of Following");
System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit");
option4 = keyboard.nextInt();
{
while (option4 !=1 || option4 != 2 || option4 !=3 || option4 !=4)
{ if (option2 ==1)
{
System.out.println("Good job you're in part (44)");}
if (option4 ==2)
{System.out.println("Good job you're in part 44.2"); }
if (option4 ==3)
{System.out.println("Good job you're in part 44.3"); }
if (option4==4)
{System.exit(0);}
break; }}
}
}
- 06-25-2011, 07:41 PM #2
I need to figure out what you're trying to do here: Do you want to prompt for options 1-4 four times (then repeat this cycle), or do you want to prompt for options 1-4 indefinitely until option 4 is chosen? There is a difference, most notably that with the first choice you will receive four different options variables; with the latter you will instead have only one option variable that will be handled each time.
If you want the first, wrap a while loop around all four options that executes forever; when the program exits the loop will obviously stop. Like so:
If you want the second, ax three of your existing while loops and put a while loop around the remaining one. Like so:Java Code:while true { while loop 1 { pick an option1 } while loop 2 { pick an option2 } while loop 3 { pick an option3 } while loop 4 { pick an option4 } }
Java Code:while true { while loop 1 { pick an option } }
- 06-25-2011, 07:51 PM #3
I want to be asked for options 1-4 indefinitely until option 4 is chosen.
I will use the following code - thanks to you- but like I said, I'm very new to Java.
while true {
while loop 1 {
pick an option
}
}
----------------------------------
Where do I put the statements and the other loops for option 2,3 or 4?
My project is based on the first chapters of the textbook and it doesn't explain the "while true" thing.
thanks in advance
- 06-25-2011, 11:27 PM #4
Thanks for the help Zack.
It helped me solve the problem.
- 06-26-2011, 02:15 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
A better approach would be to use simple switches inside a do ... while loop
This will get input, do something based on input if items are 1-3, otherwise it will test if option 4 was chosen, if so it breaks out of the loop.Java Code:do{ get user input switch(user input){ case 1: do stuff for option 1 break; case 2: do stuff for option 2 break; case 3: do stuff for option 3 break; } if(option 4) break; } while(true);
Similar Threads
-
for loop in java
By newjava in forum New To JavaReplies: 7Last Post: 12-16-2009, 10:39 AM -
Java: While loop help
By alfredoooddd in forum New To JavaReplies: 3Last Post: 11-05-2008, 03:48 AM -
Help with loop in java
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:36 AM -
Help, loop with java
By cachi in forum New To JavaReplies: 5Last Post: 08-01-2007, 06:03 AM -
Enhanced For loop In Java
By goldhouse in forum Advanced JavaReplies: 1Last Post: 05-06-2007, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks