【问题标题】:Switch and If Else CombinedSwitch 和 If Else 组合
【发布时间】:2015-10-18 01:00:08
【问题描述】:

我想知道这个程序? 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");

        }
}

【问题讨论】:

  • 您可以将 if/else 嵌套在 switch 中,也可以毫无问题地在 if/else 中进行 switch。所以你将不得不继续寻找问题:)
  • 也不要在单个输入流上打开多个扫描仪。

标签: java if-statement switch-statement logical-operators


【解决方案1】:

问题不在于同时使用 switch 语句和 if 语句。这没有什么问题。

这里有一个错误:

(hourHome == 3 || 4 || 5)

45 不是布尔表达式,不能这样使用。您可能的意思是:

(hourHome == 3 || hourHome == 4 || hourHome == 5)

【讨论】:

    【解决方案2】:

    您将需要更改代码中的一些内容

    改变:

    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 使用单个 Scanner

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多