使用 switch case 的 Java 计算器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26692689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:04:36  来源:igfitidea点击:

Java calculator using switch case

javacalculator

提问by Katty Hymanson

My requirement - calcmethod should take two numbers from main, and calcwill perform all the operations. Everything goes fine until a problem occurs in switchcommand. I get the error "choice cannot be resolved to a variable".

我的要求 -calc方法应该从 中获取两个数字maincalc并将执行所有操作。一切顺利,直到switch命令出现问题。我收到错误“无法将选择解析为变量”。

import java.util.Scanner;

public class Learn {

    public static void main(String args[]) {
        int firstnumber, secondnumber, choice;

        System.out.println("1- Add");
        System.out.println("2- Sub");
        System.out.println("3- Div");
        System.out.println("4- Mul");
        System.out.print("Enter your choice -");
        Scanner var = new Scanner(System.in);

        choice = var.nextInt();
        System.out.print("Enter first number -");
        firstnumber = var.nextInt();
        System.out.print("Enter second number -");
        secondnumber = var.nextInt();
        calc(firstnumber, secondnumber);
    }

    public static void calc(int x, int y) {
        int c;

        switch (choice) {
            case 1:
                c = x + y;
                System.out.print("Output-" + c);
                break;

            case 2:
                c = x - y;
                System.out.print("Output-" + c);
                break;

            case 3:
                c = x / y;
                System.out.print("Output-" + c);
                break;

            case 4:
                c = x * y;
                System.out.print("Output-" + c);
                break;
        }
    }
}

What am I missing, and how can I fix this?

我错过了什么,我该如何解决这个问题?

回答by dasblinkenlight

Since choiceis a local in a different function, you need to pass it as a parameter:

由于choice是不同函数中的局部变量,因此您需要将其作为参数传递:

public static void calc(int x, int y, int choice) {
    ...
}
...
calc (firstnumber,secondnumber, choice);

Note that your calcmethod is not optimal: all four cases have the same line in them:

请注意,您的calc方法不是最佳方法:所有四个cases 中都有相同的行:

System.out.print("Output-" + c);

You could move this line to after the switch, and add a default case to throw an exception when the choice is invalid.

您可以将此行移至 之后switch,并添加默认情况以在选择无效时引发异常。

回答by J0e3gan

You will need to pass choiceas an argument to calcif you wish to use its value in calc- something like this tweak to your code:

如果您希望使用它的值,则需要将其choice作为参数传递给- 对您的代码进行如下调整:calccalc

import java.util.Scanner;

public class Learn {

    public static void main(String args[]) {
        int firstnumber, secondnumber, choice;

        System.out.println("1- Add");
        System.out.println("2- Sub");
        System.out.println("3- Div");
        System.out.println("4- Mul");
        System.out.print("Enter your choice -");
        Scanner var = new Scanner(System.in);

        choice = var.nextInt();
        System.out.print("Enter first number -");
        firstnumber = var.nextInt();
        System.out.print("Enter second number -");
        secondnumber = var.nextInt();
        calc(choice, firstnumber, secondnumber); // 3rd arg added for choice
    }

    public static void calc(int choice, int x, int y) { // 3rd param added for choice
        int c;

        switch (choice) {
            case 1:
                c = x + y;
                System.out.print("Output-" + c);
                break;

            case 2:
                c = x - y;
                System.out.print("Output-" + c);
                break;

            case 3:
                c = x / y;
                System.out.print("Output-" + c);
                break;

            case 4:
                c = x * y;
                System.out.print("Output-" + c);
                break;
        }
    }
}