2015-10-17 93 views
3

我只是想循環此程序與用戶輸入「是」。我想我會在整個程序中使用一個do/while循環,但沒有任何反應。這裏是我的代碼:使用Java輸入用戶輸入循環程序

public static void main(String[] args) { 
    int num1, i; 
    Scanner Scan = new Scanner(System.in); 
    do { 
     do { 
      System.out.print("Enter a number between 20 and 30 ---> "); 
      num1 = Scan.nextInt(); 
     } while (num1 > 30 || num1 < 20); 

     for (i = num1; i >= 20; i--) { 
      System.out.print(i + " "); 
     } 
     System.out.println(""); 
    } while (Scan.equals("yes")); 
} 
+2

在向用戶顯示數字列表後,您不要求用戶輸入「YES」,因此「Scan」仍然包含他輸入的數字。 – Siddhartha

回答

2
public static void main(String[] args) { 
    int num1, i; 
    String choice; 
    Scanner Scan = new Scanner(System.in); 

    do { 
     do { 
      System.out.print("Enter a number between 20 and 30 ---> "); 
      num1 = Scan.nextInt(); 
     } while (num1 > 30 || num1 < 20); 

     for (i = num1; i >= 20; i--) { 

      System.out.print(i + " "); 
     } 

     System.out.println(""); 
     choice = Scan.next(); 
    } 
    while (choice.equals("yes")); 
} 

您需要讀取用戶輸入的選擇。