0

我有一个必须使用 switch 的任务。当输入整数 -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

5 回答 5

0

您的程序正在请求输入,但在您切换1-7时它将无法输入,如果那样的话。您只需要移除和放置. 那么完整的解决方案将是:7day % 7day = 7day % 7 = 0day % 7day

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);
}
于 2013-10-31T05:15:05.303 回答
0

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

但是,您的逻辑永远不会达到“案例 7”,因为您使用“天 %7”,它将在除以 7 后返回余数,因此如果天为正,则它将在 0 和 6 之间,如果天为负,它将在 -6 和 0 之间

于 2013-10-31T05:29:42.823 回答
0

试试这个检查一天是否大于1,然后将其发送到switch case。

 if(day>=1){
    System.exit(0);
    }else{
switch(day%7){
case:
}
}
于 2013-10-31T05:09:34.050 回答
0

为什么你甚至需要%?您已经明确处理了每种情况...让默认值处理其他所有情况。

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

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

于 2013-10-31T05:24:53.153 回答
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));
              }
于 2013-10-31T05:31:33.513 回答