这是我做的第一个项目,所以我知道有很多错误。我正在尝试使用嵌套的if else语句创建一个计算器。我得到的唯一错误是在if语句中我得到的是我不能从char转到boolean。我不知道为什么它认为我试图改为布尔但我',猜猜“= A”等可能让它认为呢?任何建议将不胜感激。
public static void main(String[] args) { //main class
char result = 0; //define and declare variables
double firstNumber = 0;
double secondNumber = 0;
double answer = 0;
System.out.println("Calculator Program\n"); //Text for user
System.out.println("Choose how to proceed:\n");
System.out.println("A. Add\n");
System.out.println("B. Subtract\n");
System.out.println("C. Multiply\n");
System.out.println("D. Divide\n");
Scanner userInput = new Scanner(System.in); //create new Scanner
result = userInput.next().charAt(0); //Take only the first letter entered
if (result = A){
System.out.println("Please enter the first number: ");
firstNumber = userInput.nextDouble(); //redefine first number
userInput.next(); //clears line
System.out.println("Please enter the second number: ");
secondNumber = userInput.nextDouble(); //redefine second number
userInput.next(); //clears line
answer = firstNumber + secondNumber; //algorithim
System.out.println("Answer: " + firstNumber + " + " + secondNumber + " = " + answer);//print answer
}
else if ( result = B){
System.out.println("Please enter the first number: ");
firstNumber = userInput.nextDouble(); //redefine first number
userInput.next(); //clears line
System.out.println("Please enter the second number: ");
secondNumber = userInput.nextDouble(); //redefine second number
userInput.next(); //clears line
answer = firstNumber - secondNumber; //algorithim
System.out.println("Answer: " + firstNumber + " - " + secondNumber + " = " + answer);//print answer
}
else if ( result = C){
System.out.println("Please enter the first number: ");
firstNumber = userInput.nextDouble(); //redefine first number
userInput.next(); //clears line
System.out.println("Please enter the second number: ");
secondNumber = userInput.nextDouble(); //redefine second number
userInput.next(); //clears line
answer = firstNumber * secondNumber; //algorithim
System.out.println("Answer: " + firstNumber + " * " + secondNumber + " = " + answer);//print answer
}
else if ( result = D){
System.out.println("Please enter the first number: ");
firstNumber = userInput.nextDouble(); //redefine first number
userInput.next(); //clears line
System.out.println("Please enter the second number: ");
secondNumber = userInput.nextDouble(); //redefine second number
userInput.next(); //clears line
answer = firstNumber / secondNumber; //algorithim
System.out.println("Answer: " + firstNumber + " / " + secondNumber + " = " + answer);//print answer
}
}
}
答案 0 :(得分:1)
char
无法转换为boolean
。
好像你想要检查一下。你可以做点什么,
String sum = "0+3";
boolean isPlus = (sum.charAt(1) == '+');
isPlus
现在将成立,因为sum
的索引1处的字符为'+'
答案 1 :(得分:0)
在if块中,您将A分配给结果。 你应该这样做(结果==' A')//' A'不是A
答案 2 :(得分:0)
你这样检查:
if (result == 'A')
将A
更改为'A'
,将=
更改为==
答案 3 :(得分:0)
为了匹配字符,您需要将if语句更改为if(result =='A')而不是if(result == A)
以下代码将为您提供更清晰的信息:
import java.util.Scanner;
public class checkBoolToChar {
public static void main(String[] args) {
char a = 0;
Scanner in = new Scanner(System.in);
a = in.next().charAt(0);
if(a == 'A'){
System.out.println("Match ");
}
}
}
答案 4 :(得分:0)
当您将条件写为
时 if(result = A)
它实际上是将值A分配给结果变量。现在,由于A不是用引号编写的,它假设A是一个变量,但由于它没有在程序中的任何地方声明,因此它假定它是一个布尔值。但结果的数据类型是char。因此它会为您提供数据类型不匹配错误。如果你把它写成
if(result == "A")
它将检查字母是否为A并且错误将被删除。