2

私はJavaでのプログラミングに不慣れです。

ユーザーが入力したsystem.out.println日付を、それが「st」、「th」、「rd」、「nd」であるかどうかを知らせたいのですが。したがって、生まれた日付に「13」を入力すると、「th」が追加されて「13th」になります。

すべての数字「1」から「31」でこれを自動的に行うにはどうすればよいですか?

並列配列を使用し、st、nd、rd、thに「1」から「31」[0]-[30]の部分と、[0]-[3]の部分を含める必要がありますか?それに応じてそれらを一致させますか?

もしそうなら、そうでなければ、どうやってそれなどを宣言しますか?

ひどく書かれた質問でごめんなさい。自分の考えを理解するのは難しいです。

4

5 に答える 5

1
public static void main(String[] args) {
        String[] dates = {"","1st","2nd","3rd","4th",...,"31st"};
        int input = 24;
        System.out.println(dates[input]);
}
于 2013-02-03T01:05:13.650 に答える
1

私はむしろそのようにします:

String getExtension(int day) {

  switch(day) {

    case 1:
    case 21:
    case 31:
    return "st";

    case 2:
    case 22:
    return "nd";

    case 3:
    case 23:
    return "rd";

    default:
    return "th";
  }

}


String formatDay(int day) {
   return day + getExtension(day);
}
于 2013-02-03T01:07:47.727 に答える
0

switch ステートメントを使用することをお勧めしますが、必ず 11、12、および 13 を含めるように注文してください。

switch (day) {
case 2:
case 22:
    suffix = "nd";
    break;
case 3:
case 23:
    suffix = "rd";
    break;
default:
    //Accounts for all other numbers including 1, 11, 12, 13, 21, and 31
    suffix = "st";
    break; //Because I'm a conformist.
}
于 2013-02-03T01:12:00.483 に答える
0

あなたがそうする必要はないと思います。日付へのこの種の追加については、明確に定義された規則があります。

  • 日付が 1 で終わり、10 未満または 20 より大きい場合は、'st' で終わります。
  • 日付が 2 で終わり、10 未満または 20 より大きい場合は、'nd' で終わります。
  • 日付が 3 で終わり、10 未満または 20 より大きい場合は、'rd' で終わります。
  • 他のすべての日付は「th」で終わります。

これが私のひどく過剰に設計されたソリューションです。それからあなたが望むものを取りなさい。

public String appendDateEnding(String theDate) {
    if(null != theDate) {
        try {
            return appendDateEnding(Integer.parseInt(theDate));
        } catch (NumberFormatException e) {
            // we'll return null since we don't have a valid date.
        }
    }
    return null;
}

private String appendDateEnding(int theDate) {
    StringBuffer result = new StringBuffer();
    if(32 <= theDate || 0 >= theDate) {
        throw new IllegalArgumentException("Invalid date.");
    } else {
        result.append(theDate);
        int end = theDate % 10;
        if(1 == end && isValidForSuffix(theDate)) {
            result.append("st");
        } else if(2 == end && isValidForSuffix(theDate)) {
            result.append("nd");
        } else if(3 == end && isValidForSuffix(theDate)) {
            result.append("rd");
        } else {
            result.append("th");
        }
    }
    return result.toString();
}

private boolean isValidForSuffix(int theDate) {
    return (10 > theDate || 20 < theDate);
}

1 から 31 までの範囲の日付が指定された場合、出力される内容は次のとおりです。

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st
于 2013-02-03T01:26:55.447 に答える
0

switch ステートメントを使用します

 int day;
    // no read in or assign day: 1 - 31

String ext;
// day % 10 (modulo 10) reduces to 0-9
switch (day % 10) {
case 1: ext = "st"; 
  break;
case 2: ext = "nd"; 
  break;
case 3: ext = "rd"; 
  break;
default: ext = "th";
  break;
}
if (day >= 11 && day <==13) ext == "th";
String dayText = "day: " + day + ext;
于 2013-02-03T01:02:07.947 に答える