Java - 在switch块中放置if语句

时间:2013-10-31 05:07:59

标签: java if-statement switch-statement

我有一项任务,我必须使用开关。当输入整数-1时,程序应该终止。 1到7之间的任何数字都会显示一周中的某一天。将显示1到7和-1之间的任何其他数字:“仅接受1到7之间的数字”。 如何在输入-1时终止程序。这是迄今为止的代码:

    Scanner s = new Scanner(System.in);
    String res = "";
    System.out.println("Input a number ");
    int day = s.nextInt();

    if(day==-1){
System.exit(0);
}  
    switch (day) {
      case 1:
        res="Today is Sunday";
      break;
      case 2: 
        res="Today is Monday";
        break;
      case 3: 
        res = "Today is Tuesday";
        break;
      case 4: 
        res = "Today is Wednesday";
        break;
      case 5:
        res = "Today is Thursday";
        break;
      case 6: 
        res = "Today is Friday";
        break;
      case 7:
        res = "Today is Saturday";
        break;
      default:
        res = "Only numbers from 1 to 7 are accepted ";
        break;
    }
   System.out.println(res);

  }
}

使用我的解决方案更新了代码。

4 个答案:

答案 0 :(得分:0)

您的计划要求输入1-7,但在转换7时,如果day % 7然后day = 7,则输入day % 7 = 0无效。您需要删除day % 7并仅放置day。然后完整的解决方案将是:

System.out.println("Input a number ");
int day = s.nextInt();

switch (day) {

  case -1:
     System.exit(0);
  break;
  case 1:
    res="Today is Sunday";
  break;
  case 2: 
    res="Today is Monday";
    break;
  case 3: 
    res = "Today is Tuesday";
    break;
  case 4: 
    res = "Today is Wednesday";
    break;
  case 5:
    res = "Today is Thursday";
    break;
  case 6: 
    res = "Today is Friday";
    break;
  case 7:
    res = "Today is Saturday";
  default:
    res = "Only numbers from 1 to 7 are accepted ";
    break;
  }
  System.out.println(res);
}

答案 1 :(得分:0)

为什么你甚至需要%?你已经明确地处理了每一个案例......让默认处理其他所有案例。

Switch(day)
{
case 1:
..
default:
}

如果你给你的代码8,那么8%7 == 1所以你得到今天是星期天。

答案 2 :(得分:0)

作为Sage的回答,你可以把case -1:System.exit(0);打破;或者如果逻辑在方法中,则使用return语句(我认为使用return语句比使用System.exit()更优雅。)

然而你的逻辑永远不会达到'case 7',因为你使用'day%7'它会在除以7之后返回残余,所以如果day为正数或者在-6和0之间它将在0到6之间日是负面的

答案 3 :(得分:0)

使用do while应该可以帮助您获得所需的行为

            {
                Scanner s = new Scanner(System.in);
                String res = "";
                Integer day;
                do{
                System.out.println("Input a number ");
                day = s.nextInt();

                System.out.println(day);
                switch (day) {
                  case 1:
                    res="Today is Sunday";
                  break;
                  case 2: 
                    res="Today is Monday";
                    break;
                  case 3: 
                    res = "Today is Tuesday";
                    break;
                  case 4: 
                    res = "Today is Wednesday";
                    break;
                  case 5:
                    res = "Today is Thursday";
                    break;
                  case 6: 
                    res = "Today is Friday";
                    break;
                  case 7:
                    res = "Today is Saturday";
                  default:
                    res = "Only numbers from 1 to 7 are accepted ";
                    break;
                }
               System.out.println(res);
            }
            while (!day.equals(-1));
              }