java中的计算器

最近我研究过可以加,减,乘和除的简单计算器。

public static void main(String arr[]){ double num1, num2; String ans = null; System.out.println("Calculator manu:"); System.out.println("\n"+"a for adding"); System.out.println("b for substracting"); System.out.println("c for multiplying"); System.out.println("d for dividing"); Scanner NumInput = new Scanner(System.in); System.out.println("\n"+"Enter number one: "); num1 = NumInput.nextDouble(); System.out.println("\n"+"Enter number two: "); num2 = NumInput.nextDouble(); while(true) { Scanner AnsInput = new Scanner(System.in); System.out.println("\n"+"Choose operation."); String answer = AnsInput.next(); if(AnsInput.equals("1")) { answer = Double.toString(num1+num2); System.out.println("\n"+"The sum of the first and second number is: "+answer); } else if(AnsInput.equals("2")) { answer = Double.toString(num1-num2); System.out.println("\n"+"Subtraction of the first and the second number is: "+answer); } else if(AnsInput.equals("3")) { answer = Double.toString(num1*num2); System.out.println("\n"+"The product of the first and second number is: "+answer); } else if(AnsInput.equals("4")) { answer = Double.toString(num1/num2); System.out.println("\n"+"Ratio of the first and the second number is: "+answer); } } } 

但是,如果我想要像普通计算器那样工作的程序呢? 它增加,减去,乘以,除以……,但不仅仅是两个而是多个数字。

这当然可以做到! 虽然它需要实质性的框架。 但是,如果你想使用这种类型的简单代码,你需要做的是将数字存储在其他地方,在这个函数之外,以及你应用操作的每两个数字,你存储它并将它用作第一个参数你想要使用的第三个。 等等。 这有意义吗?