Java计算器使用开关盒

时间:2014-11-01 20:05:47

标签: java calculator

我的要求 - calc方法应该从main获取两个数字,而calc将执行所有操作。一切顺利,直到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;
        }
    }
}

我缺少什么,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

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

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

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

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

您可以将此行移至switch之后,并添加默认大小写以在选项无效时抛出异常。

答案 1 :(得分:0)

如果您希望在choice中使用其值,则需要将calc作为参数传递给calc - 类似于对您的代码进行调整:

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;
        }
    }
}