Switch 和 If Else 组合
Switch and If Else Combined
我想知道这个程序? if else 和 switch 可以结合使用吗?我认为这可能是程序的最后一部分不能 运行 除了发生错误的原因,如果没有从代码中删除逻辑 OR。
public static void main(String[] args) {
int houseType;
int hourHome;
Scanner input = new Scanner(System.in);
System.out.println("Please select your type of residence: ");
System.out.println("Press 1 for an apartment");
System.out.println("Press 2 for a house");
System.out.println("Press 3 for a dormitory");
houseType = input.nextInt();
switch (houseType) {
case 1:
System.out.println("You entered you reside in an apartment");
break;
case 2:
System.out.println("You entered you reside in a house");
break;
case 3:
System.out.println("You entered you reside in a dormitory");
break;
default:
System.out.println("You have entered an invalid selection");
break;
}
Scanner input2 = new Scanner(System.in);
System.out.println("Please select the average number of hours you're home per day ");
System.out.println("Press 1 for 18 hours or more");
System.out.println("Press 2 for 10 to 17 hours");
System.out.println("Press 3 for 8 to 9 hours");
System.out.println("Press 4 for 6 to 7 hours");
System.out.println("Press 5 for 0 to 5 hours");
hourHome = input.nextInt();
switch (hourHome) {
case 1:
System.out.println("You entered you are home for 18 hours per day or more");
break;
case 2:
System.out.println("You entered you are home between 10 and 17" +
"hours per day");
break;
case 3:
System.out.println("You entered you are home between 8 and 9 hours per day");
break;
case 4:
System.out.println("You entered you are home between 6 and 7 hours per day");
break;
case 5:
System.out.println("You entered you are home between 0 and 5 hours per day");
break;
default:
System.out.println("You have entered an invalid selection");
break;
}
if (houseType == 1 && hourHome == 1) {
System.out.println("Based on your selections, it's recommended you get pot-bellied pig");
} else if (houseType == 1 && hourHome == 2) {
System.out.println("Based on your selections, it's recommended you get a dog");
} else
if (houseType == 1 && ((hourHome == 3 || 4 || 5)) {
System.out.println("Based on your selections, it's recommended you get a snake");
}
}
问题不在于同时使用 switch 语句和 if 语句。这没有错。
你这里有错误:
(hourHome == 3 || 4 || 5)
4
和 5
不是布尔表达式,不能那样使用。你的意思可能是:
(hourHome == 3 || hourHome == 4 || hourHome == 5)
您需要更改代码中的一些内容
变化:
hourHome = input.nextInt();
至:
hourHome = input2.nextInt();
变化:
(hourHome == 3 || 4 || 5)
至:
else if (houseType == 1 && ((hourHome == 3 || hourHome == 4 || hourHome == 5))){
是的,您可以在 switch 语句中嵌套 if/else。
您也可以对 hourHome 和 houseType 使用单个扫描仪
我想知道这个程序? if else 和 switch 可以结合使用吗?我认为这可能是程序的最后一部分不能 运行 除了发生错误的原因,如果没有从代码中删除逻辑 OR。
public static void main(String[] args) {
int houseType;
int hourHome;
Scanner input = new Scanner(System.in);
System.out.println("Please select your type of residence: ");
System.out.println("Press 1 for an apartment");
System.out.println("Press 2 for a house");
System.out.println("Press 3 for a dormitory");
houseType = input.nextInt();
switch (houseType) {
case 1:
System.out.println("You entered you reside in an apartment");
break;
case 2:
System.out.println("You entered you reside in a house");
break;
case 3:
System.out.println("You entered you reside in a dormitory");
break;
default:
System.out.println("You have entered an invalid selection");
break;
}
Scanner input2 = new Scanner(System.in);
System.out.println("Please select the average number of hours you're home per day ");
System.out.println("Press 1 for 18 hours or more");
System.out.println("Press 2 for 10 to 17 hours");
System.out.println("Press 3 for 8 to 9 hours");
System.out.println("Press 4 for 6 to 7 hours");
System.out.println("Press 5 for 0 to 5 hours");
hourHome = input.nextInt();
switch (hourHome) {
case 1:
System.out.println("You entered you are home for 18 hours per day or more");
break;
case 2:
System.out.println("You entered you are home between 10 and 17" +
"hours per day");
break;
case 3:
System.out.println("You entered you are home between 8 and 9 hours per day");
break;
case 4:
System.out.println("You entered you are home between 6 and 7 hours per day");
break;
case 5:
System.out.println("You entered you are home between 0 and 5 hours per day");
break;
default:
System.out.println("You have entered an invalid selection");
break;
}
if (houseType == 1 && hourHome == 1) {
System.out.println("Based on your selections, it's recommended you get pot-bellied pig");
} else if (houseType == 1 && hourHome == 2) {
System.out.println("Based on your selections, it's recommended you get a dog");
} else
if (houseType == 1 && ((hourHome == 3 || 4 || 5)) {
System.out.println("Based on your selections, it's recommended you get a snake");
}
}
问题不在于同时使用 switch 语句和 if 语句。这没有错。
你这里有错误:
(hourHome == 3 || 4 || 5)
4
和 5
不是布尔表达式,不能那样使用。你的意思可能是:
(hourHome == 3 || hourHome == 4 || hourHome == 5)
您需要更改代码中的一些内容
变化:
hourHome = input.nextInt();
至:
hourHome = input2.nextInt();
变化:
(hourHome == 3 || 4 || 5)
至:
else if (houseType == 1 && ((hourHome == 3 || hourHome == 4 || hourHome == 5))){
是的,您可以在 switch 语句中嵌套 if/else。
您也可以对 hourHome 和 houseType 使用单个扫描仪