我正在尝试使用类和通过它们传递值来计算器。
我发现的问题是,当我编译时,程序没有给我任何答案。
这是我的代码:
package apps;
import java.util.Scanner;
public class Apps
{
public static void main(String[] args) throws Exception
{
System.out.println("1.Add");
System.out.println("2.Subtract");
System.out.println("3.Divide");
System.out.println("4.Multiply");
String Input;
String Input1;
String Input2;
int N1;
int N2;
Scanner UserIn = new Scanner(System.in);
Input = UserIn.nextLine();
char aChar = Input.charAt(0);
System.out.println("Enter First number");
Input1 = UserIn.nextLine();
N1 = Integer.parseInt(Input1);
System.out.println("Enter Second number");
Input2 = UserIn.nextLine();
N2 = Integer.parseInt(Input2);
Calculator Calc = new Calculator();
if (aChar == 1){
System.out.println(Calc.Addition(N1,N2));
} else if (aChar == 2){
System.out.println(Calc.Subtraction(N1,N2));
}else if (aChar == 3){
System.out.println(Calc.Division(N1,N2));
}
}
}
package apps;
public class Calculator
{
public int Addition(int N1, int N2)
{
//num 1 + num2
int totalA = N1 + N2;
return totalA;
}
public int Subtraction(int N1, int N2)
{
//num 2 - num1
int totalS = N1 - N2;
return totalS;
}
public int Division(int N1, int N2)
{
int totalD = N2 / N1;
return totalD;
}
}
谢谢你的帮助。