【问题标题】:"Mistaken" result of calculation. JAVA. So stuck“错误”的计算结果。爪哇。所以卡住了
【发布时间】:2020-06-18 17:54:06
【问题描述】:

大家好。 我被卡住了,不明白为什么代码会给我一个“错误”的答案。

我的任务:编写一个程序,该程序接受数字并计算它们,直到用户键入“退出”, 然后程序打印出用户刚刚插入的所有数字的摘要并停止。

我现在在网上学java,不知道怎么回答: 当你输入1个或2个或3个或更多的数字时,它计算并打印错误的结果,甚至代码无法编译并弹出错误。

例如你输入了 2 个数字并且有错误

线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“exit” 在 java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) 在 java.base/java.lang.Integer.parseInt(Integer.java:652) 在 java.base/java.lang.Integer.parseInt(Integer.java:770) 在 JR.constructors.Solution.main(Solution.java:11)


OR 3 个数字,例如 2 + 3 + 4,它会打印 6 而不是 8。

请帮帮我!

这是我的代码

public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int sum = 0;
    while(true){
        int a = Integer.parseInt(reader.readLine());
        String s = reader.readLine();
        sum += a;
        if(s.equals("exit")){
            System.out.println(sum);
            break;
        }
    }
}

【问题讨论】:

  • "exit" 看起来不像 parseInt() 的数字。
  • 进入“exit”时跟随数据流。在s.equals("exit") 检查之前将它提供给哪里。
  • 请有人解释得更清楚
  • 使用调试器跟踪您的代码,或者只用手指指向每一行。您创建了 BufferedReader。您将sum 初始化为零。现在你开始一个循环。读取一行 - 如果没有可用的行,它会等待。将您刚刚读取的行解析为 Integer 并将其分配给 a — 如果它不是 Integer,您将收到 NumberFormatException。现在将另一行读入s。请注意,您尚未进行计算。将a 添加到sum。检查s 是否为“退出”...如果您键入的第二行是数字怎么办?该数字与“退出”进行比较,它在计算中从未使用。等

标签: java


【解决方案1】:

如果有人键入除数字以外的任何内容Integer.parseInt() 将失败并抛出错误,因此使用 1 变量从流中获取输入,首先查看用户是否键入了“退出” " 如果不尝试将字符串转换为整数,请确保将其放入 try catch 块中,以在用户键入无效内容时捕获错误。

【讨论】:

  • @Max — 仅使用一个变量也可以解决您的计算“错误”的问题
【解决方案2】:

问题:

  1. 使用reader.readLine() 两次。
  2. 退出标准放置错误。

解决方案:

  1. Integer#parseInt 中删除reader.readLine()
  2. 将退出条件放在循环的开头,这样程序就可以退出而不尝试解析单词exit,这将抛出NumberFormatException,因为这个函数只能解析整数字符串。

    代码:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Main {
        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            int sum = 0;
            while (true) {
                String s = reader.readLine();
                if (s.equals("exit")) {
                    System.out.println(sum);
                    break;
                }
                int a = Integer.parseInt(s);
                sum += a;
            }
        }
    }
    

    示例运行:

    2
    3
    4
    exit
    9
    

【讨论】:

    【解决方案3】:

    提到的错误(异常)来自不正确的输入解析。您必须明确区分读取数字和读取字符串,或者在将字符串转换为数字时明智地捕获异常。

    我建议使用Scanner 代替解决方案(更简单、更清晰、更少的异常处理)。

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
    
        String inputStr = null;
        int sum = 0;
    
        // Attention: order of conditions matters
        while(!"exit".equals(inputStr) && scn.hasNext()) {
            if(scn.hasNextInt()) {
                sum += scn.nextInt();
            }
            else {
                inputStr = scn.next();
            }
        }
    
        System.out.println("SUM="+sum);
        System.exit(0);
    }
    

    【讨论】:

    • 需要成为 BufferedReader 作为任务的规则,但谢谢!
    • 没有提到的Arf。但不客气!享受 Java
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多