简体   繁体   中英

Java - Putting an if Statement in a Switch block

I have an assignment where I have to use switch. The program is supposed to terminate when the integer -1 is entered. And any number between 1 and 7 will display a day of the week. any other number between 1 and 7 and -1 will display: "Only numbers from 1 to 7 are accepted". How can I make the program terminate when -1 is entered. Here is the code so far:

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

  }
}

updated the code with my solution.

try this check whether the day gets greater then the 1,then send it to switch case.

 if(day>=1){
    System.exit(0);
    }else{
switch(day%7){
case:
}
}

Your program is asking for input 1-7 but it will not work for input 7 as you are switching day % 7 , if day = 7 then day % 7 = 0 . You need to remove day % 7 and place only day . Then the complete solution will be:

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

Why do you even need the %? You're already explicitly handling every case...let the default handle everything else.

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

If you give your code 8, well 8%7 == 1 so you'd get Today is Sunday.

As Sage's answer, you can put case -1: System.exit(0); break; or use return statement if the logic is in a method (I think using return statement is more graceful than using System.exit()).

However your logic never reach the 'case 7', since you use 'day %7' it will return remnant after dividing by 7, so it will be between 0 and 6 if day is positive or between -6 and 0 if day is negative

Using a do while should help you get your required behaviour

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM