【问题标题】:Custom Exception Catching and Returning Incorrect Values to The Terminal自定义异常捕获并向终端返回不正确的值
【发布时间】:2020-12-16 21:03:00
【问题描述】:

使用此代码,我的目标是在终端中显示一个可以分配“等级”的 ID 号码数组。创建它以便它可以处理异常并在发生这种情况时显示消息。在代码的最终输出中,必须列出数组中的所有五个变量,超过“100”的变量被列为“0”。问题是,每当引发此异常时,文本都不会输出到终端,并且会将所有其余的“等级”设置为“0”。我想知道是否有任何方法可以避免这种情况并在“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);
    }
}

【问题讨论】:

    标签: java for-loop exception java.util.scanner


    【解决方案1】:

    您需要将输入分配给数组的当前索引。此外,如果出现异常,您需要减少计数器。

    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);
        }
    }
    

    示例运行:

    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
    

    【讨论】:

      【解决方案2】:

      你的异常永远不会被抛出。没有为scores 数组的任何元素分配任何内容,因此它们保持为0。因此表达式scores[x]==HIGHLIMIT 永远不会为真。也不应该是scores[x] &gt; HIGHLIMIT吗?

      【讨论】:

      • 我不相信代码让输出保持为零,因为在 try{} 语句中它收集数据,并打印在 'System.out.println(outString) ;'
      • 在inString = input.next(); 之后试试这个scores[x] = Integer.parseInt(inString);。
      • 看来这已经是原始代码的一部分了,我只是碰巧删除了它。非常感谢!
      • 对不起,另一个答案中的另一个@ArvindKumarAvinash 建议使用scores[x] = input.nextInt();,这是在单独的语句中解析 int 的捷径。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2023-03-18
      • 1970-01-01
      • 2012-02-12
      相关资源
      最近更新 更多