You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将日期打印程序中的if-else语句转换为switch语句的求助

Fixing Your Date Printer with Switch Statements

Hey there! Let's walk through building your date printer program using switch statements instead of if-else—since you're new to coding, we'll keep it simple and break everything down step by step.

What We're Building

We need a program that takes two integers (month number 1-12, day number 1-31) and outputs a formatted date like January 2nd or December 31st.

Step 1: Convert Month Number to Name with Switch

First, let's handle the month conversion. Switch is perfect here because we have exact integer matches (1=January, 2=February, etc.):

// Convert month number to full name
String monthName;
switch(month) {
    case 1:
        monthName = "January";
        break;
    case 2:
        monthName = "February";
        break;
    case 3:
        monthName = "March";
        break;
    case 4:
        monthName = "April";
        break;
    case 5:
        monthName = "May";
        break;
    case 6:
        monthName = "June";
        break;
    case 7:
        monthName = "July";
        break;
    case 8:
        monthName = "August";
        break;
    case 9:
        monthName = "September";
        break;
    case 10:
        monthName = "October";
        break;
    case 11:
        monthName = "November";
        break;
    case 12:
        monthName = "December";
        break;
    default:
        monthName = "Invalid Month"; // Handle bad input
        break;
}

Pro tip: Don't forget the break statements—without them, the code will "fall through" to the next case, which is almost never what you want here.

Step 2: Add Ordinal Suffixes (st/nd/rd/th) with Switch

Next, we need to add the correct suffix to the day. This has a few tricky edge cases (like 11th, 12th, 13th even though they end with 1/2/3), so we'll handle those exceptions first before using switch:

// Get ordinal suffix for day
String suffix;
// Edge case: 11,12,13 always use "th" no matter what
if (day >= 11 && day <= 13) {
    suffix = "th";
} else {
    switch(day % 10) { // Check the last digit of the day
        case 1:
            suffix = "st";
            break;
        case 2:
            suffix = "nd";
            break;
        case 3:
            suffix = "rd";
            break;
        default:
            suffix = "th";
            break;
    }
}

Step 3: Full Working Program

Putting it all together, here's the complete code with user input handling (we'll use Scanner to grab the two integers you mentioned):

import java.util.Scanner;

class DatePrinter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Get month input from user
        System.out.print("Enter month number (1-12): ");
        int month = scanner.nextInt();
        
        // Get day input from user
        System.out.print("Enter day number (1-31): ");
        int day = scanner.nextInt();
        
        // Convert month number to name
        String monthName;
        switch(month) {
            case 1: monthName = "January"; break;
            case 2: monthName = "February"; break;
            case 3: monthName = "March"; break;
            case 4: monthName = "April"; break;
            case 5: monthName = "May"; break;
            case 6: monthName = "June"; break;
            case 7: monthName = "July"; break;
            case 8: monthName = "August"; break;
            case 9: monthName = "September"; break;
            case 10: monthName = "October"; break;
            case 11: monthName = "November"; break;
            case 12: monthName = "December"; break;
            default: monthName = "Invalid Month"; break;
        }
        
        // Get correct ordinal suffix for day
        String suffix;
        if (day >= 11 && day <= 13) {
            suffix = "th";
        } else {
            switch(day % 10) {
                case 1: suffix = "st"; break;
                case 2: suffix = "nd"; break;
                case 3: suffix = "rd"; break;
                default: suffix = "th"; break;
            }
        }
        
        // Print final result or error message
        if (!monthName.equals("Invalid Month") && day >= 1 && day <= 31) {
            System.out.println(monthName + " " + day + suffix);
        } else {
            System.out.println("Invalid date! Please enter a valid month (1-12) and day (1-31).");
        }
        
        scanner.close();
    }
}

Common Mistakes to Avoid

  • Forgetting break in switch cases: This causes unintended "fall-through" (e.g., if you skip break after case 1, it will set monthName to both January and February).
  • Skipping input validation: Always add a default case in switch to catch bad month values, and check that days are within 1-31.
  • Ignoring the 11/12/13 edge cases: These are easy to miss, but they always use "th" instead of st/nd/rd.

内容的提问来源于stack exchange,提问作者JScannell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:51:26