【问题标题】:How to add clear option to my command line calculator [duplicate]如何向我的命令行计算器添加清除选项[重复]
【发布时间】:2022-01-15 09:59:58
【问题描述】:

我正在制作一个 CLI 计算器,我正在尝试添加一个清除按钮,但无法弄清楚如何清除所有用户输入并从头开始显示。我尝试使用重置方法,但似乎没有成功。

        System.out.println("Enter any of the following:");
        System.out.println(" 1 2 3 4 5 6 7 8 9 ");
        System.out.println(" + - * / = ");
        System.out.println("Enter AC to reset ");

        double firstNum = input.nextDouble();
        input.nextLine();

        if (input.equals(clear)) {
            input.reset();

        }
        System.out.println("Display 1: " + firstNum);
        System.out.println("Display 2: " + firstNum);

        System.out.println("Operator: ");
        String operand = input.nextLine();

        System.out.println("Display 1: " + firstNum + " " + operand);
        System.out.println("Display 2: " + firstNum  );

        if (operand.equals("=")) {
            break;
        }

        System.out.println("Enter your next number: ");
        double secondNum = input.nextDouble();
        input.nextLine();

        calculate(firstNum, operand, secondNum);

        System.out.println("Display 1: " + firstNum + " " + operand + " " + secondNum );
        firstNum = answer;
        System.out.println("Display 2: " + answer);


         }
          System.out.println("Answer: " + answer);
        }

【问题讨论】:

    标签: java command-line-interface calculator


    【解决方案1】:

    我想这是不可能的。我试图解决这个问题,这是浪费时间。如果有帮助,您可以使用特殊字符,如“\b”删除前一个字符或“\r”从字符串的开头写入。但是当你按回车时,你不能改变上一行。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2011-08-25
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    相关资源
    最近更新 更多