You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java计算器代码执行一次运算后首条打印语句重复循环两次求助

问题解决方案

故障原因

该异常并非循环或switch语句逻辑错误导致,核心是Java Scanner输入类的方法特性冲突:

  • 读取操作符选择时使用的nextLine()方法会读取到换行符为止,并且会主动消费掉末尾的换行符
  • 读取运算数字时使用的nextDouble()方法仅读取数值内容,不会处理输入数字后按下的回车换行符,这个换行符会残留在输入缓冲区中
  • 首次运算结束后回到循环头部,nextLine()会直接读取到缓冲区残留的换行符,得到空字符串,走完一次无效循环后立刻进入下一轮有效循环,表现为首条提示连续打印两次。

修复方案

只需要在每次运算结束后,主动消费掉缓冲区残留的换行符即可,最简单的修改方式是在switch代码块结束后、for循环结束前,新增一行sc.nextLine()调用,修改后的代码如下:

Scanner sc = new Scanner(System.in);

for (int i = 1; i>0; i++) {
    System.out.print("Type in the name of the type of Equation you want to solve on the line below: (Addition, Subtraction, Multiplication, Division, Exponents)    If you want to Stop using the calculator, type 'Close'");
    System.out.println("");
    String Input = sc.nextLine();

    if (Input.equals("Close")) {
        break;
    }

    switch (Input) {
        case "Addition" : 
            System.out.print("Type in your first number: ");
            Double Addition1 = sc.nextDouble();
            System.out.print("Type in what you want to add your first number by: ");
            Double Addition2 = sc.nextDouble();
            Double Sum = Addition1 + Addition2;
            System.out.println(Addition1 + " + " + Addition2 + " = " + Sum);
            System.out.println("");
            break;
        case "Subtraction" :
            System.out.print("Type in your first number: ");
            Double Subtraction1 = sc.nextDouble();
            System.out.print("Type in what you want to subtract your first number by: ");
            Double Subtraction2 = sc.nextDouble();
            Double Difference = Subtraction1 - Subtraction2;
            System.out.println(Subtraction1 + " - " + Subtraction2 + " = " + Difference);
            System.out.println("");
            break;
        case "Multiplication" :
            System.out.print("Type in your first number: ");
            Double Multiplication1 = sc.nextDouble();
            System.out.print("Type in what you want to multiply your first number by: ");
            Double Multiplication2 = sc.nextDouble();
            Double Product = Multiplication1 * Multiplication2;
            System.out.println(Multiplication1 + " * " + Multiplication2 + " = " + Product);
            System.out.println("");
            break;
        case "Division" :
            System.out.print("Type in your first number: ");
            Double Division1 = sc.nextDouble();
            System.out.print("Type in what you want to divide your first number by: ");
            Double Division2 = sc.nextDouble();
            Double Quotient = Division1 / Division2;
            System.out.println(Division1 + " / " + Division2 + " = " + Quotient);
            System.out.println("");
            break;
    }
    // 新增这行,消费nextDouble()残留的换行符
    sc.nextLine();
}

可选优化方案

也可以统一使用nextLine()读取所有输入,再手动将数字输入转换为Double类型,从根源上避免不同输入方法的特性冲突,示例:
Double Addition1 = Double.parseDouble(sc.nextLine());

内容的提问来源于stack exchange,提问作者Josh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:27:01