【问题标题】:Java data output "date" and "month"Java数据输出“日期”和“月份”
【发布时间】:2018-11-24 17:18:37
【问题描述】:

我只是在寻找一个程序,要求用户以“int”格式输入日期和月份,例如“1”代表天,“1”代表月份将给出“1 January”。

一开始就简单,因为可以将 1 到 12 链接到“一月”等,但需要有第二个字符串来限制每个月的天数。

此外,必须有一个子字符串才能将“st”“nd”插入到最终输出中,例如如果天数是“13”,则输出将是“13th”。

目前有以下几点:

import java.util.Scanner;

public class q1 {

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a month: ");
    String first = input.nextLine();
    System.out.print("Enter a day: ");
    String second = input.nextLine();       
    input.close();

    if (first.charAt(0) == '1') {
        first = "January";
    } else if (first.charAt(0) == '2') {
        first = "February";
    else if (first.charAt(0) == '3') {
        first = "March";
    {
    System.out.println("The date entered is the" second + "of " + first);}
    }
}

感谢您的帮助。

【问题讨论】:

  • 欢迎来到 Stack Overflow!你似乎在要求我们为你做功课。我们不会这样做,因为这样做对任何人都没有好处,从长远来看,它实际上会对您造成净伤害。请查看您的课堂笔记,并随时edit 这个问题更具体地说明您遇到的困难。另请阅读:An open letter to students with homework problems
  • 你到底卡在哪里了?
  • 我只是卡在哪里包括一个特定的部分来限制并要求用户再次输入天数,如果输入“32”并且是一月,会假设“如果> = 31”一月份的逻辑是什么?完成“伪代码”只是为了限制它。

标签: java input output


【解决方案1】:

对于您需要附加到您的一天的子字符串,您可以查看一种地图类型的数据结构。

Map<Integer, String> daySubstringMap = new TreeMap<>();

{1: "st"}
{2: "nd"}

或者

Map<Set<Integer>, String> daysSubstringMap  = new HashMap<>()

{[1]: "st"}
{[2]: "nd"}
{[4,5,6,7]:"th"}

【讨论】:

  • 谢谢,这可以在最终输出中通过连接引用吗?
  • 您可以将其用作静态地图并在生成字符串时引用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 2021-12-26
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多