程序代码:
import javax.swing.JOptionPane;
public class TryCatchTest {
public static void main(String args[])
{
String grade;
grade=JOptionPane.showInputDialog("请输入学生成绩:");
int num=0;
try{
num=Integer.parseInt(grade);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"输入错误!请输入0-100之间的整数");
System.exit(0);
}
try
{
if(num<0||num>100)
throw new Exception();
if(num>=0||num<=100)
{
if(num<=100&&num>=90)
JOptionPane.showMessageDialog(null, "优");
if(num<90&&num>=80)
JOptionPane.showMessageDialog(null,"良");
if(num<80&&num>=70)
JOptionPane.showMessageDialog(null,"中");
if(num<70&&num>=60)
JOptionPane.showMessageDialog(null,"及格");
if(num<60)
JOptionPane.showMessageDialog(null,"不及格");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"超出范围!请输入1-100的整数");
}
}
}
2.程序运行结果
时间: 2024-10-29 04:24:35