我无法理解代码中的计算。我已经在我正在制作的这个迷你计算器中完成了一种加法方法。这纯粹是练习方法。我创建了第一个加法方法,出于某种原因,当我选择将两个数字相加的选项时,程序进入该方法并返回一个答案,它执行乘法而不是加法。
整个程序的代码如下:
import java.util.Scanner;
public class Testing {
/**
* @param args
*/
public static void main(String[] args) {
Scanner userReader = new Scanner(System.in);
int number1;
int number2;
int decision;
int result = 0;
System.out.println("Please input your first number");
number1 = userReader.nextInt();
System.out.println("Please input your second number");
number2 = userReader.nextInt();
System.out.println("Please select what you would like to do");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Division");
System.out.println("4.Multiplication");
decision = userReader.nextInt();
switch(decision)
{
case 1 : result = Addition(number1,number2);
case 2 : result = number1 - number2;
case 3 : result = number1 / number2;
case 4 : result = number1 * number2;
}
System.out.println("Your answer is " + result);
}
public static int Addition(int number1, int number2) {
int Additionresult;
Additionresult = number1 + number2;
return Additionresult;
}
}
此致