如果用户这样说,我怎么能让我的代码再次循环



这是我的代码:

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; 
          }

你把1和0搞混了。此外,末尾的额外if和else语句也没有用。

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
}

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;
      }

如果你想再次循环,只需将while设置为true如果用户想退出,只需使用break代码;

使用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);

相关内容

  • 没有找到相关文章

最新更新