如何在此计算代码中进行计算?

时间:2018-04-01 17:20:29

标签: java calculation

我需要一点帮助。问题是输出(PS!最后一个输出行)。程序只打印最后一行的数字2。我怎样才能得到计算结果。谢谢!

输出在这里:

从以下计算中选择: 1:减法 2:补充 3:乘法 4:师 5:余下

做出选择:1

输入第一个数字:9 输入第二个数字:7

9 - 7 = 2

import java.util.Scanner;
public class Calculation
{
    public static void main(String[] args)
    {
        System.out.println("Choose from the following calculations:");
        System.out.println("1: subtraction");
        System.out.println("2: addition");
        System.out.println("3: multiplication");
        System.out.println("4: division");
        System.out.println("5: remainder");

        Scanner input = new Scanner(System.in);

        System.out.print("\nMake your choice:");
        int choice = input.nextInt(); 

        if( 1 <= choice && choice <= 5 )
        {
            System.out.print("\nType the first number: ");
            int first = input.nextInt();
            System.out.print("Type the second number: ");
            int second = input.nextInt();

            switch (choice)
            {
                case 1:
                System.out.println(+ (first - second));
                break;
                case 2:
                System.out.println(+ (first + second));
                break;
                case 3:
                System.out.println(+ (first * second));
                break;
                case 4:
                System.out.println(+ ((double)first / (double)second));
                break;  
                case 5:
                System.out.println(+ (first % second));
                break;
                default:
                break;
            }
        }
        else
        {
            System.out.println("Invalid choice");
        }
    }
}   

1 个答案:

答案 0 :(得分:0)

试试这个

System.out.println(first+"-"+second+"="+(first-second));

System.out.println(first+"+"+second+"="+(first+second));

System.out.println(first+"*"+second+"="+(first*second));

System.out.println(first+"/"+second+"="+((double)first / (double)second));

System.out.println(first+"%"+second+"="+(first%second));

P.S你没有考虑为你的部门划分零误差。