2017-09-14 58 views
-1

任何人都可以告訴我是什麼導致一個非常基本的程序中的Java錯誤我想寫?Java錯誤 - java:類,接口或枚舉預計

我接收到的錯誤表示:錯誤:(27,1)的java:類,接口,或枚舉預期

這裏是包名下面的代碼。

import java.util.Scanner; 

public class Main 
{ 
    public static void main(String[] args) 
    { 
     Scanner kbd = new Scanner(System.in); 
     String s = ""; 
     boolean isOk = true; 

     while (isOk) 
     { 
      System.out.print("Key in a numeric string (Enter minus sign - to quit): "); 
      s = kbd.nextLine(); 
      char c = s.charAt(0); 
      if (c == '-') 
      { 
       isOk = false; 
       System.out.print("You have quit the program."); 
       return; 
      } 
     } 
     } 
    } 
} 
+0

看看你的'}'(從最後的第4行) – 2017-09-14 05:25:21

+1

你在代碼的最後有一個額外的右括號。 –

+0

謝謝蒂姆,那很快就有幫助! –

回答

0
import java.util.Scanner; 

public class Main 
{ 
    public static void main(String[] args) 
    { 
    Scanner kbd = new Scanner(System.in); 
    String s = ""; 
     boolean isOk = true; 

     while (isOk) 
     { 
      System.out.print("Key in a numeric string (Enter minus sign - to quit): "); 
      s = kbd.nextLine(); 
      char c = s.charAt(0); 
      if (c == '-') 
      { 
       isOk = false; 
       System.out.print("You have quit the program."); 
       return; 
      } 
     } //<-- Remove this closing bracket (extraneous) 
     } 
    } 
} 

你有外來右括號。

+1

謝謝大家的快速回復,謝謝Randall。 –

+0

沒問題,容易錯過 - 小小的bug子手。 ;) –

1

您有一個額外的大括號

刪除你的最後'}'

我手動測試了代碼,非常簡單,工作正常。

+0

您可以隨時使用IDE編寫程序,例如Eclipse ...編譯器可幫助您檢測並幫助您避免在巨大代碼中出現此類錯誤。 –