String" Switch Case"简单的代码

时间:2018-04-28 17:51:38

标签: java

我刚开始学习自己的Java。想到写一个"切换案例"程序喜欢从用户获取输入字符串并想到显示日期。即使它在我的Eclipse IDC中没有显示任何错误,但我的下面的程序没有运行。

有人能告诉我下面的程序中有什么错误吗?

package MyExercies;

import java.util.Scanner;

public class sampleSwitchCase 

{

    public static void main(String[] args) 
    {
        Scanner S = new Scanner(System.in);
        String Day = S.nextLine();
        int weekday = Integer.valueOf(Day);
        S.close();      
        switch(weekday)
        {
        case 1:     
            System.out.println ("The given day is Week begining day - Monday");
            break;

        case 2:
        case 3:
        case 4:
            System.out.println ("The given day is Mid of Weekday");
            break;

        case 5:
            System.out.println ("The given day is Weekend - Friday");
            break;

        case 6:
        case 7:
            System.out.println ("The given day is End of the Week");
            break;

        }

    }   

}

3 个答案:

答案 0 :(得分:-1)

一切都很好并且正在使用您的代码!

只需添加:

 default :
            System.out.println("Write something default  here");

答案 1 :(得分:-1)

在您的代码中添加default个案,并尝试这样:

switch(weekday)
        {
        case 1:     
            System.out.println ("The given day is Week begining day - Monday");
            break;
        default:
            System.out.println("default case");

答案 2 :(得分:-2)

default块中添加switch个案。例如:

switch(exp) {
   case 1:
      break;
   default:
      break;
}