jeudi 16 février 2017

"Switch" statements containing "if" statements using "string" and "int" variables [duplicate]

This question already has an answer here:

I'm trying to use switch statements containing "if" statements in order to show that when an integer such as "1" is entered and the month I attempt to correlate it with like "January" is entered, the program shows they are a match, but when two different variables are entered such as"2" and December" are entered the program outputs "They don't match". I'm not 100% sure what it is I'm doing wrong. Any help is appreciated.

import java.util.Scanner;

public class Lab5
{
   public static void main (String[] args)
   {
      int monthInt;
      String monthName;

      Scanner scan = new Scanner(System.in);

      // Get a month in integer and a month name
      System.out.print ("Enter a month using an integer (between 1 and 12):   \n");
      monthInt = scan.nextInt();
      System.out.print ("Enter a month using its name: \n");
      monthName = scan.next();

      //set up switch statements for the numbers 1 through 12
      //in each case, use an if statement to check if the variable monthName    contains
      //their month name, and print either "They match!" or "They don't   match!"


/// COMPLETE THE REST

  switch (monthInt)
  {

  case 1: if (monthInt == 1)
          {
              if (monthName == "January")
              {
                  System.out.println("They match");
              }
          } break;
  default: System.out.println("They don't match");



 }


   }
}

Aucun commentaire:

Enregistrer un commentaire