错误无法在 java 中的 if else if 语句中将 int 转换为布尔值
error cannot convert int to boolean in the if else if statement in java
套票票价;
导入java.util.Scanner;
publicclass票价{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" Enter Class ");
System.out.println(" a. Senior b. Student c. Regular ");
System.out.println("Enter class : ");
int num = input.nextInt();
if (num = 1) {
double fare = 9 * 0.10;
System.out.println("Your fare will be " + fare + "▒. Thank you.");
}
else if (num = 2 ) {
double fare = 9 * 0.08;
System.out.println("Your fare will be " + fare + "▒. Thank you.");
}
else {
System.out.println("your fare will be 9▒. Thank you.");
}
}
}
嗨,我似乎不明白为什么我的 if 语句中有一个错误,指出它不能将 int 转换为 boolean。我已经检查过了,仍然可以找到任何问题或者我只是不知道。
您应该将 if(num=1) 更改为 if(num==1),将 if else(num=2) 更改为 if else(num==2)。
套票票价;
导入java.util.Scanner;
publicclass票价{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" Enter Class ");
System.out.println(" a. Senior b. Student c. Regular ");
System.out.println("Enter class : ");
int num = input.nextInt();
if (num = 1) {
double fare = 9 * 0.10;
System.out.println("Your fare will be " + fare + "▒. Thank you.");
}
else if (num = 2 ) {
double fare = 9 * 0.08;
System.out.println("Your fare will be " + fare + "▒. Thank you.");
}
else {
System.out.println("your fare will be 9▒. Thank you.");
}
}
}
嗨,我似乎不明白为什么我的 if 语句中有一个错误,指出它不能将 int 转换为 boolean。我已经检查过了,仍然可以找到任何问题或者我只是不知道。
您应该将 if(num=1) 更改为 if(num==1),将 if else(num=2) 更改为 if else(num==2)。