java - 为什么这会打印两次答案?

标签 java

我写了一个简单的计算器,但它打印了两次答案。因此,如果我写 7 + 2,然后选择 (1)(加法),它会打印两次 7。

import java.util.Scanner;
public class first {
    public static void main(String[] args) {
        int num1;
        int num2;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter first number and second number:");
        num1 = input.nextInt();
        num2 = input.nextInt();
        int ans;
        System.out.println("Enter your selection: 1 for addition, 2 for subtraction, 3 for multiplication and 4 for division:");
        int choose;
        choose = input.nextInt();
        switch (choose){
            case 1:
                System.out.println(num1 + num2);
                break;

            case 2:
                System.out.println(num1 - num2);
                break;

            case 3:
                System.out.println(num1 * num2);
                break;

            case 4:
                System.out.println(num1 / num2);
                break;
        }


        System.out.println(num1 + num2);

    }
}

这就是我执行 2 + 5: 7 时得到的结果 7

最佳答案

您在 switch-case 语句中有一个 print 语句,然后在 switch-case 后面又有一个 print 语句。两个打印语句,两个输出。

取下开关盒后面的那个,它应该可以正常工作

关于java - 为什么这会打印两次答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44190213/

相关文章:

java - 在Java中如何创建关联数组结构并排序,同时保留具有不同值对的重复键

java - Oracle jdbc 中能否将多个语句放入一个查询字符串中?

java - @Async @Aspect @AfterReturnung。我的方法有 HttpServletRequest 对象作为方法参数,但能够访问它我的 aop 方法

java - hibernate中M-M关系中的记录顺序不正确

java - JML 不是空变体?

Java/Java EE - 在单一环境中部署来自不同供应商的应用程序服务器

java - 为什么你可以在不进行类型转换的情况下将 long 存储到 float 中

java - 致命异常 : AsyncTask #2 An error occured while executing doInBackground() IndexOutOfBoundsException: Invalid index 1, 大小为 1

java - 在 android 11 上将文件保存在 sd 卡上

java - 根据输入值更改Java Validator的@Pattern Regex