简体   繁体   English

自定义异常捕获并将不正确的值返回到终端

[英]Custom Exception Catching and Returning Incorrect Values to The Terminal

With this code, my objective is to display an array of id numbers in the terminal that one can assign 'grades' to.使用此代码,我的目标是在终端中显示一个可以分配“等级”的 ID 号码数组。 It is created so that it can handle exceptions and display a message when that happens.创建它以便它可以处理异常并在发生这种情况时显示一条消息。 In the final output of the code all five variables in the array must be listed, with those exceeding '100' being listed as '0'.在代码的最后 output 中,必须列出数组中的所有五个变量,超过“100”的变量被列为“0”。 The problem is, whenever this exception is raised, text does not output to the terminal, and it sets all the rest of the 'grades' to '0' as well.问题是,每当引发此异常时,文本不会 output 到终端,并且它也将“等级”的所有 rest 设置为“0”。 I would like to know if there is any way I can avoid this and get the code to output the message to the terminal during the 'for' loop, as well as avoid replacing all other values with '0' Here is the code:我想知道是否有任何方法可以避免这种情况并将代码获取到 output 在“for”循环期间向终端发送消息,以及避免用“0”替换所有其他值这是代码:

//ScoreException.Java
public class ScoreException extends Exception {
    public ScoreException(String s) {
        super(s);
    }
}
//TestScore.Java
import java.util.*;
public class TestScore {
    public static void main(String args[]) throws Exception {
        Scanner input = new Scanner(System.in);
        int[] ids = {1234, 2345, 3456, 4567, 5678};
        int[] scores = {0, 0, 0, 0, 0};
        String scoreString = new String();
        final int HIGHLIMIT = 100;
        String inString, outString = "";
        for (int x = 0; x < ids.length; ++x) {
            try{
                System.out.println("Enter score for student id number: " + ids[x]);
                inString = input.next();
                if(scores[x]>HIGHLIMIT){
                    throw new ScoreException("Score over 100");
                }
            }catch(ScoreException e){
                scores[x]=0;
                System.out.println("Score over 100");
            }
        }
                
        for (int x = 0; x < ids.length; ++x)
            outString = outString + "ID #" + ids[x] + "  Score " + scores[x] + "\n";
        System.out.println(outString);
    }
}

Your exception is never thrown.你的异常永远不会被抛出。 Nothing is assigned to any element of the scores array so they remain 0. Thus the expression scores[x]==HIGHLIMIT is never true.没有为scores数组的任何元素分配任何内容,因此它们保持为 0。因此,表达式scores[x]==HIGHLIMIT永远不会为真。 Also shouldn't it be scores[x] > HIGHLIMIT ?也不应该是scores[x] > HIGHLIMIT吗?

You need to assign the input to the current index of the array.您需要将输入分配给数组的当前索引。 Also, you need the decrease the counter in case of exception.此外,如果出现异常,您需要减少计数器。

import java.util.Scanner;

class ScoreException extends Exception {
    ScoreException(String message) {
        super(message);
    }
}

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] ids = { 1234, 2345, 3456, 4567, 5678 };
        int[] scores = { 0, 0, 0, 0, 0 };
        final int HIGHLIMIT = 100;
        String outString = "";
        for (int x = 0; x < ids.length; ++x) {
            try {
                System.out.print("Enter score for student id number, " + ids[x] + ": ");
                scores[x] = input.nextInt();// Assign the input to the current index of the array
                if (scores[x] > HIGHLIMIT) {
                    throw new ScoreException("Score over 100");
                }
            } catch (ScoreException e) {
                System.out.println("Score over 100");
                --x;// Decrease the counter
            }
        }

        for (int x = 0; x < ids.length; ++x)
            outString = outString + "ID #" + ids[x] + "  Score " + scores[x] + "\n";
        System.out.println(outString);
    }
}

A sample run:示例运行:

Enter score for student id number, 1234: 89
Enter score for student id number, 2345: 102
Score over 100
Enter score for student id number, 2345: 78
Enter score for student id number, 3456: 110
Score over 100
Enter score for student id number, 3456: 90
Enter score for student id number, 4567: 87
Enter score for student id number, 5678: 86
ID #1234  Score 89
ID #2345  Score 78
ID #3456  Score 90
ID #4567  Score 87
ID #5678  Score 86

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM