2017-07-15 33 views
-2

我对编程非常陌生,并且正在处理一个实践do/while循环和switch语句的赋值。我已经写出了一个列出博物馆时间的程序并编译,但我似乎无法让它正常工作。它只是抛出:程序在我的Do/While循环中抛出异常

import java.util.Scanner; 
public class MuseumHours { 
    public static void main (String[] args) { 

    Scanner stdIn = new Scanner(System.in); 
    //variables 
    String day; 
    boolean holiday = true; 
    String userContinue; 
    String monday = "Closed"; 
    //end variables 

    //begin program 

    do { 

    System.out.println("Hello! Thank you for visiting the museum's website. For what day would you like to view our hours of operation?"); 
    day = stdIn.nextLine(); 

    System.out.println("Is today a holiday?"); 
    holiday = stdIn.nextBoolean(); 

    switch (day) { 

     case "Monday": 
      System.out.println(monday); 
     case "Tuesday": 
      if (holiday !=true) { 
       System.out.println("The museum is open from 12:00 to 4:00."); 
      } else { 
       System.out.println("The museum is open from 1:00 to 3:00."); 
       } 
      break; 
     case "Wednesday": 
      if (holiday !=true) { 
       System.out.println("The museum is open from 12:00 to 4:00."); 
      } else { 
       System.out.println("The museum is open from 1:00 to 3:00."); 
       } 
      break; 
     case "Thursday": 
      if (holiday != true) { 
       System.out.println("Today the museum opens from 10:00 to 5:00."); 
      } else { 
       System.out.println("Today the museum opens from 11:00 to 4:00."); 
       } 
      break; 
     case "Friday": 
      if (holiday != true) { 
       System.out.println("Today the museum opens from 10:00 to 5:00."); 
      } else { 
       System.out.println("Today the museum opens from 11:00 to 4:00."); 
       } 
      break; 

     case "Saturday": 
      if (holiday != true) { 
       System.out.println("Today the museum is open from 9:00 to 6:00."); 
      } else { 
       System.out.println("Today the museum is open from 10:00 to 5:00."); 
       } 
      break; 
     case "Sunday": 
      if (holiday != true) { 
       System.out.println("Today the museum is open from 9:00 to 6:00."); 
      } else { 
       System.out.println("Today the museum is open from 10:00 to 5:00."); 
       } 
      break; 

     default: 
      System.out.println("Invalid day."); 

      } 

      System.out.println("Would you like to run the program again? Enter Y for yes, or N for no."); 
      userContinue = stdIn.nextLine(); 
     } while (userContinue.equals("Y")); 
    } 
} 
+1

_它只是抛出:_ ..你有什么异常? –

+0

抛出什么异常以及哪些输入? –

+0

周一 –

回答

1

例外可以是,当你没有在执行时从命令行传递正确类型的参数。

例如,如果任何值为“今天是假期?”,下面的行会给出错误信息。问题不同于任何这些(真,假),不区分大小写。

holiday = stdIn.nextBoolean();

希望有帮助!

0

在Java SE 7和更高版本,可以

如果你不使用的Java SE 7中的开关 语句的表达式中使用String对象,以后你不能运行这个程序

还你的代码可以通过更换

if(holiday != true){...} 

提高
if(!holiday){...}