程序在我的Do / While循环中抛出异常

时间:2017-07-15 04:53:06

标签: java switch-statement do-while

我是编程新手,正在编写一个练习do / while循环和switch语句的作业。我写了一个列出博物馆时间的程序并编译,但我似乎无法让它正常运行。它只是抛出:

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

2 个答案:

答案 0 :(得分:1)

例外情况可能是您在执行时没有从命令行传递正确类型的参数。

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

holiday = stdIn.nextBoolean();

希望有所帮助!

答案 1 :(得分:0)

  

在Java SE 7及更高版本中,您可以在交换机中使用String对象   陈述的表达

如果您没有使用java SE 7及更高版本,则无法运行此程序

您也可以通过替换

来改进代码
if(holiday != true){...}

if(!holiday){...}