2014-07-03 38 views
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; 
      } 
+0

的,什麼是不工作? – MadProgrammer

+0

構造時使用 – HJK

回答

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

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

,如果你要循環再一次,只要將同時在真實,如果用戶想退出只使用斷碼;

0

這裏使用的解決方案,同時做到:

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); 
相關問題