【发布时间】:2016-03-17 18:52:03
【问题描述】:
一直在尝试做一个简单的计算器加、减、除、乘。但它不允许我进入计算部分。即使我输入 +、-、/ 或 '',它总是显示“计算类型不正确” 如何让扫描仪将 +、-、/、'' 理解为输入?谢谢
import java.util.Scanner;
public class Calculator {
public void typeOfCalc(){
Scanner user_input = new Scanner(System.in);
System.out.print("What type of calculation do you want? \n Addition? type '+' \n Subtraction? type '-' \n Division? type '/' \n or Multiplication type '*' \n");
String calcType = user_input.next().trim();
if (calcType != "+" || calcType != "*" || calcType != "/" || calcType != "-"){
System.out.println("Incorrect type of calculation, try again \n");
typeOfCalc();
}
else if (calcType == "+"){
System.out.print("Chose your first number \n");
int num1 = user_input.nextInt();
System.out.print("Chose your second number \n");
int num2 = user_input.nextInt();
System.out.print(num1 + " + " + num2 + " = \n" + (num1 + num2) + "\n");
}
else if (calcType == "-"){
System.out.print("Chose your first number \n");
int num1 = user_input.nextInt();
System.out.print("Chose your second number \n");
int num2 = user_input.nextInt();
System.out.print(num1 + " - " + num2 + " = \n" + (num1 - num2) + "\n");
}
else if (calcType == "/"){
System.out.print("Chose your first number \n");
int num1 = user_input.nextInt();
System.out.print("Chose your second number \n");
int num2 = user_input.nextInt();
System.out.print(num1 + " / " + num2 + " = \n" + (num1 / num2) + "\n");
}
else if (calcType == "*"){
System.out.print("Chose your first number \n");
int num1 = user_input.nextInt();
System.out.print("Chose your second number \n");
int num2 = user_input.nextInt();
System.out.print(num1 + " * " + num2 + " = \n" + (num1 * num2) + "\n");
}
}
}
【问题讨论】:
-
关于您的下一个问题,请参阅How do I compare strings in Java?。
-
没用,不过还是谢谢
标签: java input calculator