2014-09-22 61 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."); 
     } 
    } 
} 
}