我实际上遇到了我的程序问题。其实我在学校学习(11年级 - 大专),所以请用非常简单的语言来解释我。我正在开发一个测验,非常基础的学校项目,所以程序就这样继续下去......我希望用户通过输入1-4的数字输入他/她的选择。我不希望用户输入字母,字母或特殊字符或任何其他编号。除了1-4之外。我尝试使用try和catch,但是我的程序在抛出异常后停止。即使在显示错误信息System.out.println(“输入无效,请在1-4之间输入一个数字”)之后,我想让程序代码运行。 示例程序如何在java中尝试并捕获异常处理后恢复代码
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
谢谢!这真的很有帮助! –