使用开关的基本计算器出现问题

Trouble with basic calculator using switch

我已经研究过这个问题,但找不到解决我问题的答案。我一直得到输出 0。对于此作业,我不允许使用任何方法。在我的程序中,1+2 等于 0

 public static void main(String[] args) {

    int result = 0;

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number");
    int number = sc.nextInt();
    System.out.println("Enter operation");
    System.out.println("1.+");
    System.out.println("2.-");
    System.out.println("3.*");
    System.out.println("4./");
    System.out.println("5.=");
    int operation = sc.nextInt();

    while (operation != 5) {
        System.out.println("Enter next number");
        number = sc.nextInt();
        System.out.println("Enter operation");
        operation = sc.nextInt();

        switch (operation) {
            case 1:
                result += number;
                System.out.println("result= " + result);
                break;
            case 2:
                result -= number;
                System.out.println("result= " + result);
                break;
            case 3:
                result *= number;
                System.out.println("result= " + result);
                break;
            case 4:
                result/ = number;
                System.out.println("result= " + result);
                break;

        }
        System.out.println(result);

    }
import java.lang.System;
import java.util.Scanner;

public class Java{
 public static void main(String args[])
 {

    int result = 0;

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number");
    result = sc.nextInt();
    System.out.println("Enter operation");
    System.out.println("1.+");
    System.out.println("2.-");
    System.out.println("3.*");
    System.out.println("4./");
    System.out.println("5.=");
    int operation = sc.nextInt();

    while (operation != 5) {
        System.out.println("Enter next number");
        int number = sc.nextInt();

        switch (operation) {
            case 1:
                result += number;
                System.out.println("result= " + result);
                break;
            case 2:
                result -= number;
                System.out.println("result= " + result);
                break;
            case 3:
                result *= number;
                System.out.println("result= " + result);
                break;
            case 4:
                result /= number;
                System.out.println("result= " + result);
                break;

        }
        System.out.println(result);
        System.out.println("Enter operation");
        operation = sc.nextInt();

    }
 }
}

对于第一个数字和第二个数字,您使用的是变量数字..所以您将得到零。

您的第一个操作没有被执行。

试试这个:

    public static void main(String[] args) {

    int result = 0;

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number");
    result = sc.nextInt();
    System.out.println("Enter operation");
    System.out.println("1.+");
    System.out.println("2.-");
    System.out.println("3.*");
    System.out.println("4./");
    System.out.println("5.=");
    int operation = sc.nextInt();

    while (operation != 5) {
        System.out.println("Enter next number");
        int operand2 = sc.nextInt();

        switch (operation) {
            case 1:
                result += operand2;
                System.out.println("result= " + result);
                break;
            case 2:
                result -= operand2;
                System.out.println("result= " + result);
                break;
            case 3:
                result *= operand2;
                System.out.println("result= " + result);
                break;
            case 4:
                result /= operand2;
                System.out.println("result= " + result);
                break;

        }
        System.out.println("Enter operation");
        operation = sc.nextInt();
    }
    System.out.println("Global result = " + result);
}

}

问题是您在循环的第一次迭代中读取 numberoperation 两次,而不是在 switch case 之后移动两个读取条件,如下所示。

public static void main(String[] args) throws java.lang.Exception {
    int result = 0;

    Scanner sc = new Scanner(System. in );
    System.out.println("Enter number");
    int number = sc.nextInt();
    System.out.println("Enter operation");
    System.out.println("1.+");
    System.out.println("2.-");
    System.out.println("3.*");
    System.out.println("4./");
    System.out.println("5.=");
    int operation = sc.nextInt();
    try {
        while (operation != 5) {


            switch (operation) {
                case 1:
                    result += number;
                    System.out.println("result= " + result);
                    break;
                case 2:
                    result -= number;
                    System.out.println("result= " + result);
                    break;
                case 3:
                    result *= number;
                    System.out.println("result= " + result);
                    break;
                case 4:
                    result /= number;
                    System.out.println("result= " + result);
                    break;

            }

            System.out.println("Enter next number");
            number = sc.nextInt();
            System.out.println("Enter operation");
            operation = sc.nextInt();


        }
        System.out.println("Final esult is " + result);
    } catch (Exception e) {
        System.out.println(e);
    }
}

输出

Enter number 2
Enter operation 1
1.+
2.-
3.*
4./
5.=
result= 2
Enter next number 5
Enter operation 5
Final result is 2

Demo