java - 该程序的 if else 语句有什么问题?

标签 java if-statement

由于某种原因,我的代码无法运行,说我有一些错误。

//ask for user input for the selected vehicle

if(choice == 'A' || 'a') {
    System.out.println("How many miles would you like to drive the van?: ");
    milesDriven = input.nextInt();  
} else if(choice == 'B' || 'b') {
    System.out.println("How many miles would you like to drive the honda?: ");
    milesDriven = input.nextInt();
} else if(choice == 'C' || 'c') {
    System.out.println("Here are the statuses of your cars!\n");
    Garage.printCarStatus();
} else {
    System.out.println("That is an invalid choice!");
}

最佳答案

更改代码中的条件 -

if(choice == 'A' || 'a') // the OR operation is not applicable of char values

if(choice == 'A' || choice == 'a') // boolean operands using OR

关于java - 该程序的 if else 语句有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461753/

相关文章:

python - 为什么以下 Python 中的简短脚本会忽略 "if"中的条件?

python - 在 pandas/python 中嵌套带有 .loc 的 if 语句

java - 如何使用@GetMapping按id加载数据

Java JFrame - 按键似乎没有注册

java - Flutter stomp 客户端无法与 spring stomp 客户端连接

java - 在 Java 中模拟 if-in 语句

php忽略重定向if语句

java - 如何从 gradle 项目中删除 java 标准库?

java - spring boot Controller 可以接收纯文本吗?

python - 防止Python在不满足条件的if语句中检查语法错误