Java 显示菜单编译错误
我只是对为什么会有编译错误以及如何修复它感到困惑。另外,在最后一个if语句中,当用户无法输入有效的整数时,我应该再次重复该菜单。我该怎么做?提前谢谢Java 显示菜单编译错误,java,menu,compiler-errors,Java,Menu,Compiler Errors,我只是对为什么会有编译错误以及如何修复它感到困惑。另外,在最后一个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.Estimat
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();
}
public类主菜单{
公共静态void main(字符串[]args)
{
println(“我的第一个Java程序可以做很多事情!”);
System.out.println(“1.估计总体\n2.生成随机整数\n3.打印ASCII表\n4.迭代近似pi”);
System.out.println(“您想做什么?(1-4)”;
System.out.print(“您的选择:”);
int输入=0;
扫描仪键盘=新扫描仪(System.in);
开关(键盘.nextInt())
{
案例1:
System.out.println(“您选择了估算人口。”);
打破
案例2:
System.out.println(“您选择生成随机整数。”);
打破
案例3:
System.out.println(“您选择打印ASCII表”);
打破
案例4:
println(“您选择通过迭代来近似pi。”);
打破
}
如果(input>4 | | input您的代码需要进行大量更改才能正常工作,那么首先需要连续循环,该循环将连续运行并等待用户输入。然后,如果输入不是整数,则必须捕获inputmashthexe选项,这必须在代码开头执行。
这是一个坏主意,提供代码,这将阻止你尝试不同的方式。我仍然附加代码
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.");
}
}
}
}
import java.util.InputMismatchException;
导入java.util.Scanner;
公开课考试{
公共静态void main(字符串[]args){
println(“我的第一个Java程序可以做很多事情!”);
System.out.println(“1.估计总体\n2.生成随机整数\n3.打印ASCII表\n4.迭代近似pi”);
System.out.println(“您想做什么?(1-4)”;
System.out.print(“您的选择:”);
扫描仪键盘=新扫描仪(System.in);
while(true){
int输入=0;
试一试{
输入=键盘.nextInt();
}捕获(输入不匹配异常e){
系统输出
.println(“对不起,此菜单只允许整数。再见!”);
系统出口(0);
}
开关(输入){
案例1:
System.out.println(“您选择了估算人口。”);
打破
案例2:
System.out.println(“您选择生成随机整数。”);
打破
案例3:
System.out.println(“您选择打印ASCII表”);
打破
案例4:
println(“您选择通过迭代来近似pi。”);
打破
}
如果(输入>4 | |输入<1){
系统输出
.println(“对不起,我不知道该怎么办。请再试一次。”);
}
}
}
}
马上,我要指出,在Main菜单类之外有三条语句(从keyboard.next()开始)。stdin copy标准输入是空的编译信息Main。java:35:错误:解析时到达文件末尾}^1错误标准输出为空,以便将这三条语句移动到类中。任何类定义中最后一条应该是右大括号。我这样做了。现在它打印到“您的选择”,其余显示“Main.java:48:error:解析时到达文件结尾}”你应该考虑正确地格式化和缩进你的代码,这样检查丢失的括号和括号,这样就容易多了。