-1
我是新來的Java,請裸露在我身邊。 我只是困惑,爲什麼有一個編譯錯誤,我應該如何解決它。另外,在最後一條if語句中,當用戶輸入一個有效的整數時,我應該再次重複該菜單。我應該怎麼做?提前致謝。顯示菜單編譯錯誤
public class MainMenu {
public static void main(String[] args)
{
System.out.println("My First Java program can do many things!");
System.out.println("1.Estimate population\n2.Generate random integer\n3. Print ASCII table\n4. Approximate pi by iteration");
System.out.println("What would you like to do? (1-4)");
System.out.print("Your choice: ");
int input = 0;
Scanner keyboard = new Scanner(System.in);
switch (keyboard.nextInt())
{
case 1:
System.out.println("You chose to estimate population.");
break;
case 2:
System.out.println("You chose to generate random integer.");
break;
case 3:
System.out.println("You chose to print ASCII table.");
break;
case 4:
System.out.println("You chose to approximate pi by iteration.");
break;
}
if(input>4 || input<1)
{
System.out.println("Sorry, I don't know what to do. Please try again.");
keyboard.next();
if(!keyboard.hasNextInt())
{
System.out.println("Sorry, only integers allowed for this menu. Good-bye!");
System.exit(0);
}
keyboard.next();
input = keyboard.nextInt();
keyboard.nextLine();
}
馬上就要指出,你有三個語句(以keyboard.next();開頭)在MainMenu類之外。 – MarsAtomic 2014-09-22 22:44:28
標準輸入複製 標準輸入是空 編譯信息 Main.java:35:錯誤:到達文件末尾,而解析 } ^ 1錯誤 標準輸出 標準輸出是空 – awxf218 2014-09-22 22:47:13
所以這三個語句移動到類。任何類定義中的最後一件事應該是一個正確的大括號。 – MarsAtomic 2014-09-22 22:48:11