简体   繁体   中英

How to output a date using a switch statement with nested if-else statements

Hi this is my code that I have used for my application. Every thing works fine however in some instances when inputting data that would use case 2 the if-else statement and the nested if-else statement displays one long answer of the different System.out.print. What would I have to do to display one answer? I want the program to display if the date entered to show if its a valid date, if the date entered is a leap year as well as display an error if the day or month is entered wrong

package javaapplication18;
import java.util.Scanner;

/**
 *
 * @author Thurston Pietersen
 */
public class JavaApplication18 
{
        public static void main(String[] args) 
    {
        int sI,fI ,sL, month, day, year;
        String mm, dd, yyyy, date;
        Scanner keyboard = new Scanner(System.in);
        // TODO code application logic here
        System.out.print("Input date using the following format mm/dd/yyyy: ");
        date = keyboard.next();

        sL = date.length();

        fI = date.indexOf("/");
        sI = date.lastIndexOf("/");

        mm = date.substring(0,fI);
        month = Integer.parseInt(mm);
        dd = date.substring((fI+1),sI);
        day = Integer.parseInt(dd);
        yyyy = date.substring((sI+1),sL);
        year = Integer.parseInt(yyyy);


        switch (month)
        {  
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                if (day > 31)
                    System.out.print(date + " is an invalid date.");
                else 
                    System.out.print(date +  " is a valid date.");

            case 4:
            case 6:
            case 9:
            case 11:
                if (day > 30)
                    System.out.print(date + " is an invalid date.");
                else 
                    System.out.print(date +  " is a valid date.");
            case 2:
                if (year % 4 == 0)
                    if (day > 29)
                       System.out.print(date + " is an invalid day.");
                    else
                       System.out.print(date + " is a valid date and leap year.");
                else
                    if (day > 28)
                        System.out.print(date + " is an invalid day.");
                    else
                       System.out.print(date + " is a valid date.");
            default:
                System.out.println("The date: " + date + " has an invalid month");




        }       



    }

}

You're missing break in all your cases. Due to that, the switch falls through, and all the cases are executed. Insert break at appropriate places:

switch (month) {  
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if (day > 31)
                System.out.print(date + " is an invalid date.");
            else 
                System.out.print(date +  " is a valid date.");
            break;  // Here

        case 4:
        case 6:
        case 9:
        case 11:
            if (day > 30)
                System.out.print(date + " is an invalid date.");
            else 
                System.out.print(date +  " is a valid date.");
            break;  // Here

        case 2:
            if (year % 4 == 0)
                if (day > 29)
                   System.out.print(date + " is an invalid day.");
                else
                   System.out.print(date + " is a valid date and leap year.");
            else
                if (day > 28)
                    System.out.print(date + " is an invalid day.");
                else
                   System.out.print(date + " is a valid date.");
            break;  // Here

        default:
            System.out.println("The date: " + date + " has an invalid month");

    }       

And please make a habbit of always enclosing your if s and else s with curly braces.

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