import java.util.Scanner;
//1.编写程序接收用户输入分数信息,如果分数在0—100之间,输出成绩。
//如果成绩不在该范围内,抛出异常信息,提示分数必须在0—100之间。
public class MyTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
System.out.println("请输入分数:");
int score=sc.nextInt();
if(score<0||score>100){
throw new ScoreScopeException("分数必须在0-100之间");
}
System.out.println("分数为:"+score);
}
catch(ScoreScopeException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
public class ScoreScopeException extends RuntimeException {
public ScoreScopeException() {
super();
}
public ScoreScopeException(String message) {
super(message);
}
}
import java.util.Scanner;
/*2.写一个方法void isTriangle(int a,int b,int c),
判断三个参数是否能构成一个三角形, 如果不能则抛出异常IllegalArgumentException,
显示异常信息 “a,b,c不能构成三角形”,如果可以构成则显示三角形三个边长,
在主方法中得到命令行输入的三个整数, 调用此方法,并捕获异常。*/
public class SjxTest {
public static void main(String[] args) {
System.out.println("请输入三条边:");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
isTriangle(a,b,c);
}
public static void isTriangle(int a,int b,int c){
if(a<0||b<0||c<0){
throw new IllegalArgumentException("三边不能是负数");
}
else if(a+b>c&&a+c>b&&b+c>a){
System.out.println("可以构成三角形,三角形的三边分别是"+a+","+b+","+c);
}else{
throw new IllegalArgumentException("a,b,c不能构成三角形");
}
}
}