user3799949
user3799949

Reputation: 1

How can if make my code loop over again if the user says so?

This is my code:

public class Pizza {
    public static void main(String[] args) { 

        int orderDone = 1;
    //declare variables
        while(orderDone == 1){
          int done = 1;
          double total2 = 0;
          final int DELIVERY_COST = 3;
          double pizzaPrice = 8.50;
          String customerAddress = null;
          String customerNumber = null;
          int pizzaQuantity = 0;

    //my code

    orderDone = readInt("Would you like to make another order? (0 - yes  1 - no) ");
          if(orderDone == 1){
            orderDone = 2;
          } else {
            done = 0; 
          }

Upvotes: 0

Views: 88

Answers (3)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

Here is a solution using do while:

int orderDone = 0;

Scanner scanner = new Scanner(System.in);

do{
    int done = 1;
    double total2 = 0;
    final int DELIVERY_COST = 3;
    double pizzaPrice = 8.50;
    String customerAddress = null;
    String customerNumber = null;
    int pizzaQuantity = 0;

    //my code

    System.out.println("Would you like to make another order? (0 - yes  1 - no) ");
    orderDone = scanner.nextInt();

}
while(orderDone == 0);

Upvotes: 0

user3799958
user3799958

Reputation:

public class Pizza { public static void main(String[] args) {

    int orderDone = 1;
//declare variables
    while(true){ 
      int done = 1;
      double total2 = 0;
      final int DELIVERY_COST = 3;
      double pizzaPrice = 8.50;
      String customerAddress = null;
      String customerNumber = null;
      int pizzaQuantity = 0;

//my code

orderDone = readInt("Would you like to make another order? (0 - yes  1 - no) ");
      if(orderDone == 1){
        break;
      }

if you want to loop over again in again just set your while in true and if the user want to exit just use the break code;

Upvotes: 1

Dean Leitersdorf
Dean Leitersdorf

Reputation: 1341

Here: you had a mix up with the 1's and 0's. Also, you had no use with the extra if and else statements at the end.

public class Pizza {
public static void main(String[] args) { 

    int orderDone = 0;
//declare variables
    while(orderDone == 0){
      int done = 1;
      double total2 = 0;
      final int DELIVERY_COST = 3;
      double pizzaPrice = 8.50;
      String customerAddress = null;
      String customerNumber = null;
      int pizzaQuantity = 0;
      //my code 
      orderDone = readInt("Would you like to make another order? (0 - yes  1 - no) ");
    }
  }
 // reset of the code
}

Upvotes: 1

Related Questions