小编mar*_*ark的帖子

Java,在Try-Catch之后继续

当我运行程序时,100除以数字,但一旦达到0,就会出现一条消息告诉用户100不能除以0.

如何在错误发生后让程序继续运行,并将100除以其余数字.

这是我目前的代码:

public class testing {
    int[] table;
    int number;

    public testing(int number) {
        this.number = number;
        table = new int[number];
    }

    public static void main(String[] args) {
        try {
            int[] numbers = { 2, 3, 0, 4, 5 };
            testing division;
            for (int i = 0; i < 5; i++) {
                division = new testing(100 / numbers[i]);
                System.out.println("Answer is " + division.number);
            }
        } catch (Exception e) {
            System.out.println("A number cannot be divided by 0");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java try-catch

-4
推荐指数
1
解决办法
349
查看次数

标签 统计

java ×1

try-catch ×1