我的程序实际上有问题。实际上我在学校学习(11年级 - 大专)所以请用非常简单的语言解释我。我正在开发一个测验,非常基本的学校项目,所以程序像这样继续......我希望用户通过输入 1-4 的数字来输入他/她的选择。我不希望用户输入字母、字母或特殊字符或任何其他号码。除了1-4。我尝试使用 try 和 catch,但我的程序在抛出异常后停止。我希望程序代码即使在显示错误消息 System.out.println(" Invalid input. Please enter a number between 1-4"); 后也能运行。 示例程序
import java.io.*;
import java.lang;
public class
{
public static void main()throws IOException
{
InputStreamReader read =new InputStreamReader (System.in);
BufferedReader in =new BufferedReader (read);
System.out.println(" Select your category");
System.out.println(" 1. Food");
System.out.println(" 2. National");
System.out.println("");
System.out.println(" Enter your choice");
int choice=Integer.parseInt(in.readLine());
if(choice==1)//food category
{
int score = 0;
System.out.println("what are dynamites made?");
System.out.println("1. peanuts");System.out.println("2. grapes");
System.out.println("3. flaxseeds");System.out.println("4. fish");
System.out.println(" Enter your choice");
int food1= Integer.parseInt(in.readline());
if(c1=1)
{ System.out.println(" Correct answer");
score=score+10
}
else
{
System.out.println(" Wronge answer");
score=score+0
}
//then i have the second question with the same format
}
if(choice==2)//natioanl category with the same format as above
{//two question with if else statements in them }
}
}// also help me where to add the try and catch statements