如果用户这样说,如果让我的代码重新循环怎么办?

时间:2014-07-03 03:17:47

标签: java loops variables block replay

这是我的代码:

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

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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;
      }

如果你想再次循环,只需将你的时间设置为true,如果用户想要退出,只需使用中断代码;

答案 2 :(得分: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);