求一个JAVA的关于异常处理的程序,要求输入年龄,超过100岁程序异常,谢谢.....

如题所述

public class Hello{
public static void main(String[] args){
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入年龄...");
try{
int age = sc.nextInt();
if(age > 100){
throw new Exception("年龄超过100岁了。。");
}
}catch(Exception e){
System.out.println("年龄超过100岁了。。");
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-24
public class Test
{
Scanner sc=new Scanner(System.in);

int age=sc.nextInt();

if(age>100)
{
throw new RunntimeException("年龄超过100岁");

}
}

这个代码我没编译,就在这个网页上面写得,不过就是这个意思啦。
第2个回答  2013-04-24
把刚才那哥们儿中间的代码写在main方法里就行了。