2015-10-18 91 views
0

我在想这个程序吗?是否有可能,如果其他和开关可以结合?我认为这可能是为什么程序的最后一部分不能从发生的错误中排除,如果没有从代码中删除逻辑或。开关和其他组合

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"); 

     } 
} 
+0

可以嵌套的if/else在交换机或交换机中的if/else没有问题。所以你将不得不继续寻找问题:) –

+0

也不要在一个输入流上打开多个扫描仪。 – chrylis

回答

0

您需要更改代码中的几件事情

变化:

hourHome = input.nextInt(); 

到:

hourHome = input2.nextInt(); 

变化:

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

到:

else if (houseType == 1 && ((hourHome == 3 || hourHome == 4 || hourHome == 5))){ 

是的,你可以嵌套的if/else在switch语句。

您还可以使用单一的扫描仪两个hourHome和houseType

1

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

您这里有一个错误:

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

45不是布尔表达式,不能这样使用。你大概的意思是:

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