2014-09-22 92 views
-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(); 
} 
+0

馬上就要指出,你有三個語句(以keyboard.next();開頭)在MainMenu類之外。 – MarsAtomic 2014-09-22 22:44:28

+0

標準輸入複製 標準輸入是空 編譯信息 Main.java:35:錯誤:到達文件末尾,而解析 } ^ 1錯誤 標準輸出 標準輸出是空 – awxf218 2014-09-22 22:47:13

+0

所以這三個語句移動到類。任何類定義中的最後一件事應該是一個正確的大括號。 – MarsAtomic 2014-09-22 22:48:11

回答

0

您的代碼需要很多更改才能使其工作,首先需要連續循環,它將持續運行並等待用戶輸入。然後,如果輸入不是整數,則必須捕獲InputMismathexeption,您必須在代碼的開頭執行該操作。 提供代碼是一個壞主意,它會停止嘗試不同的方式。我仍然附上代碼。

import java.util.InputMismatchException; 
import java.util.Scanner; 
public class Test { 
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: "); 

    Scanner keyboard = new Scanner(System.in); 
    while (true) { 
     int input = 0; 
     try { 
      input = keyboard.nextInt(); 
     } catch (InputMismatchException e) { 
      System.out 
        .println("Sorry, only integers allowed for this menu. Good-bye!"); 
      System.exit(0); 
     } 
     switch (input) { 
     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."); 
     } 
    } 
} 
}