Home > database >  i was printing months using switch case while taking input from user. but the output is coming alway
i was printing months using switch case while taking input from user. but the output is coming alway

Time:06-18

import java.util.Scanner;

public class SwitchCase {
  public static void main(String[] args) {
    
    int month;
    System.out.println("Please Enter Months Number : ");
    Scanner s=new Scanner(System.in);
    month= s.nextInt();

    switch (month){
      case 1:
        System.out.println("January");
        break;
      case 2:
        System.out.println("January"); break;
      case 3:
        System.out.println("January"); break;
      case 4:
        System.out.println("January"); break;
      case 5:
        System.out.println("January"); break;
      case 6:
        System.out.println("January"); break;
      case 7:
        System.out.println("January"); break;
      case 8:
        System.out.println("January"); break;
      case 9:
        System.out.println("January"); break;
      case 10:
        System.out.println("January"); break;
      case 11:
        System.out.println("January"); break;
      case 12:
        System.out.println("January"); break;
      default : System.out.println("Please enter valid month number ! Between 1-12.");
    }

  }
}

Whenever i gave input between 1-12 its always printing case 1.

CodePudding user response:

In each case statement, you are printing the exact same output.

Is month equal to 4? Print "January".

Is month equal to 11? Print "January".

Every input prints "January".

So to fix "4", for example, change this:

  case 4:
    System.out.println("January"); break;

to this:

  case 4:
    System.out.println("April"); break;

To fix "11", change this:

  case 11:
    System.out.println("January"); break;

to this:

  case 11:
    System.out.println("November"); break;

CodePudding user response:

In every case statement, you are printing the same value as "January" that is the reason you are getting the same value.

Solution:

public class SwitchDemo {
    public static void main(String[] args) {

        int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
    }
}
  •  Tags:  
  • java
  • Related