0

这是我的代码:

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

3 回答 3

1

在这里:您混淆了 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
}
于 2014-07-03T03:21:28.003 回答
1

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,如果用户想退出,只需使用中断代码;

于 2014-07-03T03:31:19.790 回答
0

这是使用 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);
于 2014-07-03T03:31:59.450 回答