我正在使用开关来计算两个数字,但是当我尝试执行任何类型的运算符时,我都会不断得到0
.
//imports
import java.util.Scanner;
public class Calculator {
public static void main (String[] args) {
//get first number
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the first number: > ");
int num1 = scan.nextInt();
// get second number
System.out.println("Please enter the second number: > ");
int num2 = scan.nextInt();
// what the user wants to do
System.out.println("Please enter the method you would like to use "
+ "(+ , - , * , / , ^ ) > " );
String method = scan.next();
char operation = method.charAt(0);
String output = "";
//switch for first number
switch(num1) {
case 0: output+= "zero";
break;
case 1: output+= " one";
break;
case 2: output+= " two";
break;
case 3: output+= " three";
break;
case 4: output+= " four";
break;
case 5: output+= " five";
break;
case 6: output+= " six";
break;
case 7: output+= " seven";
break;
case 8: output+= " eight";
break;
case 9: output+= " nine";
break;
case 10: output+= " ten";
break;
}
// operation the user wants to use
switch (operation){
case '+' : output += " plus";
break;
case '-' : output += " minus";
break;
case '*' : output += " times";
break;
case '/' : output += " divided by";
break;
case '^' : output += " to the power of";
break;
}
//switch for second number
switch (num2) {
case 0: output += " zero";
break;
case 1: output += " one";
break;
case 2: output += " two";
break;
case 3: output += " three";
break;
case 4: output += " four";
break;
case 5: output += " five";
break;
case 6: output += " six";
break;
case 7: output += " seven";
break;
case 8: output += " eight";
break;
case 9: output += " nine";
break;
case 10: output += " ten";
break;
// calculate the answer
}
int answer = 0 ;
switch (answer) {
case '+' : answer = (answer + (num1 + num2));
break;
case '-' : answer = (answer + (num1 - num2));
break;
case '*' : answer = (answer + (num1 * num2));
break;
case '/' : answer = (answer + (num1 / num2));
break;
case '^' : answer = (answer + (int) (Math.pow(num1,num2) ));
break;
}
System.out.println(output + " equals " + answer );
}
}
我很确定我的问题出在程序的底部,具有实际功能
int answer = 0 ;
switch (answer) {
从那个区域的某个地方开始,只是很难弄清楚。也许其他人可以就我做错了什么提供意见
提前感谢您的帮助