2016-11-15 26 views
0

不斷收到語法錯誤,插入while表達式來完成do語句。它也許一些簡單的像大括號等我的代碼有什麼問題?盡力而爲嘗試抓住

{ 
    int num = 0; 
    //flag 
    boolean inputOk = false; 
    Scanner s = new Scanner (System.in); 

    do {    
     try { 
      System.out.println("Enter a number...."); 
      num =s.nextInt(); 

      System.out.println("you entered : " + num); 
      // got here then things are good 
      inputOk = true; 

     } catch (InputMismatchException ex) { 
      System.out.println("Again please....digits only"); 
      // flush the scanner 
      s.next(); 
     } 

    } while (inputOk != true); 
    s.close(); 
    System.out.println("Thank you");  
} 
+0

什麼是錯誤? –

+0

請檢查以下解決方案我認爲{是唯一的問題 –

+0

請標記爲答案或投票了此解決方案幫助您 –

回答

0

你缺少結束大括號「{」做我在糾正之後,下面的代碼

 int num = 0; 
    //flag 
    boolean inputOk = false; 
    Scanner s = new Scanner (System.in); 

     do { 

     try { 
      System.out.println("Enter a number...."); 
      num =s.nextInt(); 

     System.out.println("you entered : " + num); 
     // got here then things are good 
     inputOk = true; 

     } catch (InputMismatchException ex) { 
      System.out.println("Again please....digits only"); 
      // flush the scanner 
      s.next(); 


     }} while (inputOk != true); 
     { 
      s.close(); 
      System.out.println("Thank you"); 
     } 
0

在你的代碼是缺少結束花括號「}」爲做。對於掃描儀,最好使用資源嘗試。這裏是工作代碼

int num = 0; 
    //flag 
    boolean inputOk = false; 
    try (Scanner s = new Scanner(System.in)) { 
     do { 

      try { 
       System.out.println("Enter a number...."); 
       num = s.nextInt(); 

       System.out.println("you entered : " + num); 
       // got here then things are good 
       inputOk = true; 

      } catch (InputMismatchException ex) { 
       System.out.println("Again please....digits only"); 
       // flush the scanner 
       s.next(); 

      } 
     } 
     while (inputOk != true); 
    } 
     System.out.println("Thank you"); 
+0

在您的編碼器,你缺少結束捲曲括號「}」爲Do –