【问题标题】:How can I change this code to make the user enter in both the Month and the Year instead of just stopping at entering in the month?如何更改此代码以使用户同时输入月份和年份,而不是仅仅停止输入月份?
【发布时间】:2014-10-05 01:10:51
【问题描述】:

我正在努力完成这项任务: 编写一个提示用户输入年份的程序 以及月份名称的前三个字母(带有 大写的第一个字母)并显示数字 月中的天数。记住要考虑飞跃 年!

当我运行程序时,它会提示我输入一年。我随机输入一年,然后它什么也不做。有人可以帮助我,让我不仅可以输入一年,还可以输入相应的月份?

这是一个示例结果:

输入年份: 2002年 输入月份(前 3 个字母,首字母大写): 构建成功(总时间:3 秒)

我的程序在这里:

  package assignment.pkg4;

/**
 *
 * @author David Yang
 */
import java.util.Scanner;
public class DaysOfAMonth {
    public static void main(String[] args){

        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter a year
        System.out.println("Enter a year: ");

        int year= input.nextInt();

        // Prompt the user to enter the first 3 letters of a month
        System.out.println("Enter a month (first 3 letters with the first letter in uppercase): ");
        String month = input.nextLine();


        if (null != month) switch (month) {
            case "Jan":
            case "Mar":
            case "May":
            case "July":
            case "Aug":
            case "Oct":
            case "Dec":
                System.out.println(month + year + "has 31 days");
                break;
            case "Apr":
            case "Jun":
            case "Sep":
            case "Nov":
                System.out.println(month + year + "has 30 days");
                break;
            case "Feb":
                System.out.println(month + year + "has 28 days");
        }

        if (year % 4 == 0) {
            if ("Feb".equals(month))
                System.out.println(month + year + "has 29 days");
        }
    }
    } 

【问题讨论】:

    标签: java prompt


    【解决方案1】:

    input.nextInt(); 不会取行中的 \n,因此您需要明确删除它。

    int year= input.nextInt();
    input.nextLine();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多