计算器不会接受用户输入
calculator wont take user input
一直在尝试做一个简单的加减乘除计算器。但它不允许我进入计算部分。它总是显示 "incorrect type of calculation",即使我键入 +、-、/ 或“” 我如何让扫描器理解 +、-、/、“”作为输入?谢谢
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");
}
}
}
您需要使用 .equals()
来检查字符串是否相等。
因此 calcType.equals("*")
用于相等或 !calcType.equals("*")
检查它们是否相同。
字符串是不可变的,所以当你使用==
时,它会检查它是否是内存中的相同字符串,而不是检查字符串的实际内容
一旦调整代码以遵循@redFIVE 的建议(将 !=
替换为 !myVar.equals(str)
),请确保将您的 OR (||
) 替换为 AND (&&
) .
考虑以下因素:
if (!calcType.equals("+") || !calcType.equals("*") || ...)
如果输入是 "+"
,条件仍然会通过,因为 calcType
不等于 "*"
,并且逻辑计算结果为 TRUE。
当您使用 AND 时:if (!calcType.equals("+") && !calcType.equals("*") && ...)
您应该会看到预期的结果。
但是,您当前代码的主要问题是由于您滥用 !=
来比较 Strings
。解决这个问题,您至少可以使用调试器来查看 为什么 上述逻辑可能会失败,或者产生意外结果。
一直在尝试做一个简单的加减乘除计算器。但它不允许我进入计算部分。它总是显示 "incorrect type of calculation",即使我键入 +、-、/ 或“” 我如何让扫描器理解 +、-、/、“”作为输入?谢谢
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");
}
}
}
您需要使用 .equals()
来检查字符串是否相等。
因此 calcType.equals("*")
用于相等或 !calcType.equals("*")
检查它们是否相同。
字符串是不可变的,所以当你使用==
时,它会检查它是否是内存中的相同字符串,而不是检查字符串的实际内容
一旦调整代码以遵循@redFIVE 的建议(将 !=
替换为 !myVar.equals(str)
),请确保将您的 OR (||
) 替换为 AND (&&
) .
考虑以下因素:
if (!calcType.equals("+") || !calcType.equals("*") || ...)
如果输入是 "+"
,条件仍然会通过,因为 calcType
不等于 "*"
,并且逻辑计算结果为 TRUE。
当您使用 AND 时:if (!calcType.equals("+") && !calcType.equals("*") && ...)
您应该会看到预期的结果。
但是,您当前代码的主要问题是由于您滥用 !=
来比较 Strings
。解决这个问题,您至少可以使用调试器来查看 为什么 上述逻辑可能会失败,或者产生意外结果。