尚学堂编码题答案 第六章 异常机制

本编码答案为本人个人编辑,仅供参考。如有更优答案或者代码编写规范等问题欢迎读者私信本人或在下方评论处与本人交流。
1. 编写程序接收用户输入分数信息,如果分数在0—100之间,输出成绩。如果成绩不在该范围内,抛出异常信息,提示分数必须在0—100之间。
要求:使用自定义异常实现。
import java.util.Scanner;

public class ScoreException01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“请问您需要输入分数的个数:”);
int num = input.nextInt();
Student[] s = new Student[num];
for (int i = 0; i < s.length; i++) {
s[i] = new Student();
}

    for (int i = 0, j = 0; j < s.length; i++) {
        System.out.println("请输入分数:");
        int score = input.nextInt();
        try {
            s[j++].setScore(score);
        } catch (ScoreInputException e) {
            e.printStackTrace();
            j--;// 用于去除因为异常而未输入的数组中的空位
            continue;
        }
    }
    input.close();
    System.out.println("输入的分数为:");
    for (int i = 0; i < s.length; i++) {
        System.out.print(s[i].getScore() + " ");
    }
}

}

class ScoreInputException extends Exception {// 自定义一个异常
public ScoreInputException() {

}

public ScoreInputException(String message) {
    super(message);
}

}

class Student {
private int score;

public Student() {

}

public Student(int Score) {
    try {
        this.setScore(Score);
    } catch (ScoreInputException e) {
        e.printStackTrace();
    }
}

public void setScore(int score) throws ScoreInputException {// 手动抛出一个异常对象
    if (score < 0 || score > 100) {
        throw new ScoreInputException("您输入的分数有误,输入的分数必须是0-100之间,请重新输入!");
    } else {
        this.score = score;
    }
}

public int getScore() {
    return this.score;
}

}
2. 写一个方法void isTriangle(int a,int b,int c),判断三个参数是否能构成一个三角形, 如果不能则抛出异常IllegalArgumentException,显示异常信息 “a,b,c不能构成三角形”,如果可以构成则显示三角形三个边长,在主方法中得到命令行输入的三个整数, 调用此方法,并捕获异常。
import java.util.Scanner;

public class TestTriangle {
public static void main(String[] args) {
TestTriangle testTriangle = new TestTriangle();
Scanner input = new Scanner(System.in);
int[] count = new int[3];
char[] angle = { ‘a’, ‘b’, ‘c’ };
boolean flag = true;
while (flag) {
for (int i = 0; i < angle.length; i++) {
System.out.println(“请输入” + angle[i] + “的边长:”);
count[i] = input.nextInt();
}
try {
testTriangle.isTriangle(count[0], count[1], count[2]);
flag = false;
} catch (IllegalArgumentException e) {
e.printStackTrace();
flag = true;
System.out.println(“**请重新输入**”);

        }
    }
    input.close();
    System.out.println("你输入的三角形三个边长分别为:");
    for (int c : count) {
        System.out.print(c + "  ");
    }

}

class IllegalArgumentException extends Exception {
    public IllegalArgumentException() {

    }

    public IllegalArgumentException(String message) {
        super(message);
    }
}

public void isTriangle(int a, int b, int c) throws IllegalArgumentException {
    if (a + b <= c || a + c <= b || b + c <= a) {
        throw new IllegalArgumentException("a,b,c不能构成三角形");
    }
}

}
3. 编写一个计算N个学生分数平均分的程序。程序应该提示用户输入N的值,如何必须输入所有N个学生分数。如果用户输入的分数是一个负数,则应该抛出一个异常并捕获,提示“分数必须是正数或者0”。并提示用户再次输入该分数。
import java.util.Scanner;

public class ScoreAverageException03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“请问您需要输入分数的个数:”);
int num = input.nextInt();
Student01[] s = new Student01[num];
for (int i = 0; i < s.length; i++) {
s[i] = new Student01();
}

    for (int i = 0, j = 0; j < s.length; i++) {
        System.out.println("请输入分数:");
        int score = input.nextInt();
        try {
            s[j++].setScore(score);
        } catch (ScoreInputException01 e) {
            e.printStackTrace();
            j--;// 用于去除因为异常而未输入的数组中的空位
            continue;
        }
    }
    input.close();

    System.out.println("输入的分数为:");
    int sum=0;
    for (int i = 0; i < s.length; i++) {
        System.out.print(s[i].getScore() + " ");
        sum+=s[i].getScore();
    }
    double avg=(sum/num)*1.0;
    System.out.println("您输入分数的平均数为:"+avg);
}

}

扫描二维码关注公众号,回复: 3300599 查看本文章

class ScoreInputException01 extends Exception {// 自定义一个异常
public ScoreInputException01() {

}

public ScoreInputException01(String message) {
    super(message);
}

}

class Student01 {
private int score;

public Student01() {

}

public Student01(int Score) {
    try {
        this.setScore(Score);
    } catch (ScoreInputException01 e) {
        e.printStackTrace();
    }
}

public void setScore(int score) throws ScoreInputException01 {// 手动抛出一个异常对象
    if (score < 0 ) {
        throw new ScoreInputException01("您输入的分数有误,输入的分数必须是正数或者是0,请重新输入!");
    } else {
        this.score = score;
    }
}

public int getScore() {
    return this.score;
}

}

猜你喜欢

转载自blog.csdn.net/nihaoya123_/article/details/82219105