Jump to content

Need help with Loops

airfrog19

For a project, i have to create a program that can order food and add up all the totals. I am stuck on how to create a loop when the user needs to order the food. This is what i got so far:

 

 
package project.pkg1;
import java.util.Scanner;
 
public class Project1 {
 
 
    public static void main(String[] args) {
     int choice = 0;
     String order;
     Scanner Keyboard = new Scanner(System.in);
     
     
     double num;
     double GoodBurger = 7.75;
     double GoodSalad = 7.00;
     double Soda = 2.00;
     double KidsMeal = 3.00;
     double tax;
     double subtotal = 0, total;
     final double salestax = 0.875;
     
     
     System.out.println("Welcome to Good Burger!");
     System.out.println("=======================");
     System.out.println("Place your orders and type 'Finish' when you are done");
     System.out.println("--------------------------------------------------------");
    
    
     System.out.println("1. GoodBurger $7.75");
     System.out.println("2. GoodSalad $ 7.00");
     System.out.println("3. Soda $2.00");
     System.out.println("4. KidsMeal $3.00 \n");
     choice = Keyboard.nextInt();
 
     
     
    
     
       
    
}
 
What do i need to do? 
Link to comment
Share on other sites

Link to post
Share on other sites

Add a boolean for "doneOrdering" and put the item selection in a while loop.

 

While(doneOrdering == false) (Can also do "While(!doneOrdering))

{

 

}

 

When finished is entered set the boolean to true so you can continue to something else.

RIG: I7-4790k @ 4.5GHz | MSI Z97S SLI Plus | 12GB Geil Dragon RAM 1333MHz | Gigabyte G1 Gaming GTX 970 (1550MHz core/7800MHz memory) @ +18mV(Maxed out at 1650/7800 so far) | Corsair RM750 | Samsung 840 EVO 120GB, 1TB Seagate Barracuda | Fractal Design Arc Midi R2 (Closed) | Sound Blaster Z                                                                                                                        Getting: Noctua NH-D15 | Possible 250GB Samsung 850 Evo                                                                                        Need a console killer that actually shits on every console? Here you go (No MIR/Promo)

This is why you should not get an FX CPU for ANY scenario other than rendering on a budget http://linustechtips.com/main/topic/286142-fx-8350-r9-290-psu-requirements/?p=3892901 http://linustechtips.com/main/topic/266481-an-issue-with-people-bashing-the-fx-cpus/?p=3620861

Link to comment
Share on other sites

Link to post
Share on other sites

boolean done = false; 

 

while (!done){

    Take order. 
    set done = true upon completion of the order.

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Add a boolean for "doneOrdering" and put the item selection in a while loop.

 

While(doneOrdering == false) (Can also do "While(!doneOrdering))

{

 

}

 

When finished is entered set the boolean to true so you can continue to something else.

So, will it print out the list of food that the user will write until it says done?

Link to comment
Share on other sites

Link to post
Share on other sites

So, will it print out the list of food that the user will write until it says done?

It will print out the list of food and accept inputs until finished is triggered.

RIG: I7-4790k @ 4.5GHz | MSI Z97S SLI Plus | 12GB Geil Dragon RAM 1333MHz | Gigabyte G1 Gaming GTX 970 (1550MHz core/7800MHz memory) @ +18mV(Maxed out at 1650/7800 so far) | Corsair RM750 | Samsung 840 EVO 120GB, 1TB Seagate Barracuda | Fractal Design Arc Midi R2 (Closed) | Sound Blaster Z                                                                                                                        Getting: Noctua NH-D15 | Possible 250GB Samsung 850 Evo                                                                                        Need a console killer that actually shits on every console? Here you go (No MIR/Promo)

This is why you should not get an FX CPU for ANY scenario other than rendering on a budget http://linustechtips.com/main/topic/286142-fx-8350-r9-290-psu-requirements/?p=3892901 http://linustechtips.com/main/topic/266481-an-issue-with-people-bashing-the-fx-cpus/?p=3620861

Link to comment
Share on other sites

Link to post
Share on other sites

It will print out the list of food and accept inputs until finished is triggered.

do i put the list of food in inside the while loop? like how would it look like?

Link to comment
Share on other sites

Link to post
Share on other sites

do i put the list of food in inside the while loop? like how would it look like?

Does Java have a pre-defined update function that is called 60 times per second?

RIG: I7-4790k @ 4.5GHz | MSI Z97S SLI Plus | 12GB Geil Dragon RAM 1333MHz | Gigabyte G1 Gaming GTX 970 (1550MHz core/7800MHz memory) @ +18mV(Maxed out at 1650/7800 so far) | Corsair RM750 | Samsung 840 EVO 120GB, 1TB Seagate Barracuda | Fractal Design Arc Midi R2 (Closed) | Sound Blaster Z                                                                                                                        Getting: Noctua NH-D15 | Possible 250GB Samsung 850 Evo                                                                                        Need a console killer that actually shits on every console? Here you go (No MIR/Promo)

This is why you should not get an FX CPU for ANY scenario other than rendering on a budget http://linustechtips.com/main/topic/286142-fx-8350-r9-290-psu-requirements/?p=3892901 http://linustechtips.com/main/topic/266481-an-issue-with-people-bashing-the-fx-cpus/?p=3620861

Link to comment
Share on other sites

Link to post
Share on other sites

Does Java have a pre-defined update function that is called 60 times per second?

where do i find that?

Link to comment
Share on other sites

Link to post
Share on other sites

boolean done = false; 

 

while (!done){

    Take order. 
    set done = true upon completion of the order.

}

where do i write that?

Link to comment
Share on other sites

Link to post
Share on other sites

Not going to code if for you but should look something like this:

package project.pkg1;import java.util.Scanner; public class Project1 {    public static void main(String[] args) {     	 	 	 int choice = 0;     String order;     Scanner Keyboard = new Scanner(System.in);          double num;     double GoodBurger = 7.75;     double GoodSalad = 7.00;     double Soda = 2.00;     double KidsMeal = 3.00;     double tax;     double subtotal = 0, total;     final double salestax = 0.875;          System.out.println("Welcome to Good Burger!");     System.out.println("=======================");     System.out.println("Place your orders and type 'Finish' when you are done");     System.out.println("--------------------------------------------------------");    	 //WHILE(BOOL IS TRUE)	     System.out.println("1. GoodBurger $7.75");     System.out.println("2. GoodSalad $ 7.00");     System.out.println("3. Soda $2.00");     System.out.println("4. KidsMeal $3.00 \n");     choice = Keyboard.nextInt();	 	 //ADD THE CHOICE PRICE TO A VARIABLE THAT CONTAINS TOTAL	 	 //ASK USER IF THEY WANT TO ADD MORE. USE IF STATMENT TO DETERMINE.	 //IF USER WANTS TO STOP, SET BOOL TO FALSE	 } 
Link to comment
Share on other sites

Link to post
Share on other sites


package project.pkg1;

import java.util.Scanner;

 

public class Project1 {

 

 

    public static void main(String[] args) {

     int choice = 0;

     String order;

     Scanner Keyboard = new Scanner(System.in);

     

     

     double num;

     double GoodBurger = 7.75;

     double GoodSalad = 7.00;

     double Soda = 2.00;

     double KidsMeal = 3.00;

     double tax;

     double subtotal = 0, total;

     final double salestax = 0.875;

     


   System.out.println("Welcome to Good Burger!");

    System.out.println("=======================");

    System.out.println("Place your orders and type 'Finish' when you are done");

    System.out.println("--------------------------------------------------------");


 



   

    while(!done){ 

         choice = Keyboard.nextInt(); //user enters the order and then finish. 

          if (choice == 1) {

                      System.out.println("1. GoodBurger $7.75");

                       subTotal += GoodBurger;

          }

          else if (choice == 2) .............and so on. 

          else if (choice == 5) done = true; //Because you're using .nextInt() you're reading ints, not strings, so I                                                                               don't think "finish" will be read (I may be wrong though...not sure)

     }

}

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

 

package project.pkg1;
import java.util.Scanner;
 
public class Project1 {
 
 
    public static void main(String[] args) {
     int choice = 0;
     String order;
     Scanner Keyboard = new Scanner(System.in);
     
     
     double num;
     double GoodBurger = 7.75;
     double GoodSalad = 7.00;
     double Soda = 2.00;
     double KidsMeal = 3.00;
     double tax;
     double subtotal = 0, total;
     final double salestax = 0.875;
     
   System.out.println("Welcome to Good Burger!");
    System.out.println("=======================");
    System.out.println("Place your orders and type 'Finish' when you are done");
    System.out.println("--------------------------------------------------------");
 
   
    while(!done){ 
         choice = Keyboard.nextInt(); //user enters the order and then finish. 
          if (choice == 1) {
                      System.out.println("1. GoodBurger $7.75");
                       subTotal += GoodBurger;
          }
          else if (choice == 2) .............and so on. 
          else if (choice == 5) done = true; //Because you're using .nextInt() you're reading ints, not strings, so I                                                                               don't think "finish" will be read (I may be wrong though...not sure)
     }
}
 

 

what if i wanted to use words instead of numbers? can i replace 1 with something else?

Link to comment
Share on other sites

Link to post
Share on other sites

what if i wanted to use words instead of numbers? can i replace 1 with something else?

You could use TextIO.getln(); which just reads a line from input. And make the user input the name of the item being ordered. So, if he wanted to order a goodburger and a goodsalad, he would type goodburger then hit enter, the program would recognize the input and add goodburger to the order, and then he would type goodsalad and hit enter.

 

Something like this:

       while(!done){          choice = TextIO.getln(); //user enters the order and then finish.           if (choice.toUpperCase.equals("GOODBURGER")) { //.toUpperCase capitlizes the entire string --                                                                               //alleviate user input errors.                      System.out.println("1. GoodBurger $7.75");                      subTotal += GoodBurger;          }          else if (choice.toUpperCase.equals("GOODSALAD")) .............and so on.           else if (choice.toUpperCase.equals("FINISH")) done = true;          else                System.out.println("I'm sorry, I do not recognize that);     }}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

-double post - ignore

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

You could use TextIO.getln(); which just reads a line from input. And make the user input the name of the item being ordered. So, if he wanted to order a goodburger and a goodsalad, he would type goodburger then hit enter, the program would recognize the input and add goodburger to the order, and then he would type goodsalad and hit enter.

 

Something like this:

       while(!done){          choice = TextIO.getln(); //user enters the order and then finish.           if (choice.toUpperCase.equals("GOODBURGER")) { //.toUpperCase capitlizes the entire string --                                                                               //alleviate user input errors.                      System.out.println("1. GoodBurger $7.75");                      subTotal += GoodBurger;          }          else if (choice.toUpperCase.equals("GOODSALAD")) .............and so on.           else if (choice.toUpperCase.equals("FINISH")) done = true;     }}

so i did what you said but it's not working http://puu.sh/cqv6f/858f4c2fce.png  do i need another scanner or string?

Link to comment
Share on other sites

Link to post
Share on other sites

 

Not going to code if for you but should look something like this:

package project.pkg1;import java.util.Scanner; public class Project1 {    public static void main(String[] args) {     	 	 	 int choice = 0;     String order;     Scanner Keyboard = new Scanner(System.in);          double num;     double GoodBurger = 7.75;     double GoodSalad = 7.00;     double Soda = 2.00;     double KidsMeal = 3.00;     double tax;     double subtotal = 0, total;     final double salestax = 0.875;          System.out.println("Welcome to Good Burger!");     System.out.println("=======================");     System.out.println("Place your orders and type 'Finish' when you are done");     System.out.println("--------------------------------------------------------");    	 //WHILE(BOOL IS TRUE)	     System.out.println("1. GoodBurger $7.75");     System.out.println("2. GoodSalad $ 7.00");     System.out.println("3. Soda $2.00");     System.out.println("4. KidsMeal $3.00 \n");     choice = Keyboard.nextInt();	 	 //ADD THE CHOICE PRICE TO A VARIABLE THAT CONTAINS TOTAL	 	 //ASK USER IF THEY WANT TO ADD MORE. USE IF STATMENT TO DETERMINE.	 //IF USER WANTS TO STOP, SET BOOL TO FALSE	 } 

what do i write when the boolean is true? the orders?

Link to comment
Share on other sites

Link to post
Share on other sites

What IDE are you using to code? Usually if you hover over the red line it will display some suggestions.

 

If you wanna read in a String you can use Scanner.nextLine();

 

I would not recommend this though as it means validating the option would be more difficult than asking the users to make a selection from an int or a char.

 

 

As for the while loop bit based on your last post, while the condition is true keep prompting the user to make a selection, read the selection and add the order according to how you were doing it. If the user is done, set the boolean to false and handle it accordingly. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×