1

ユーザーが月または同等の数値を入力するように求められ、入力された月の数値または入力された数値に対応する月を返す小さなタスクが与えられました。制約は次のとおりです。 - いかなる種類の GUI も含めてはなりません - 入力には BufferedReader を使用する必要があります - 少なくとも 1 つの Switch ステートメントを使用する必要があります

誰かに何かアイデアがあれば、それは大歓迎です。これまでの私のコードは次のとおりです。

/**
 * Month task
 * 
 * @author Dan Foad
 * @version 0.01
 */

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Months {
    public static void main(String []args) throws IOException {
        int iInput;
        boolean isParseable;
        String szInput;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Please type either the number of the month, or the month itself to convert.");
        System.out.print("> ");
        szInput = br.readLine();

        try {
            Integer.valueOf(szInput);
            isParseable = true;
        }
        catch(Exception e) {
            isParseable = false;
        }

        if (isParseable) {
            iInput = Integer.valueOf(szInput);
            System.out.println(numberToMonth(iInput));
        }
        else {
            szInput = szInput.toLowerCase();
            System.out.println(monthToNumber(szInput));
        }
        return;
    }

    public static String numberToMonth(int iMonth) {
        String MonthReturn;
        switch(iMonth) {
            case (1): MonthReturn = "January"; break;
            case (2): MonthReturn = "February"; break;
            case (3): MonthReturn = "March"; break;
            case (4): MonthReturn = "April"; break;
            case (5): MonthReturn = "May"; break;
            case (6): MonthReturn = "June"; break;
            case (7): MonthReturn = "July"; break;
            case (8): MonthReturn = "August"; break;
            case (9): MonthReturn = "September"; break;
            case (10): MonthReturn = "October"; break;
            case (11): MonthReturn = "November"; break;
            case (12): MonthReturn = "December"; break;
            default: MonthReturn = "0"; break;
        }
        return MonthReturn;
    }

    public static int monthToNumber(String szMonth) {
        int MonthReturn;
        switch(szMonth) {
            case ("january"): MonthReturn = 1; break;
            case ("february"): MonthReturn = 2; break;
            case ("march"): MonthReturn = 3; break;
            case ("april"): MonthReturn = 4; break;
            case ("may"): MonthReturn = 5; break;
            case ("june"): MonthReturn = 6; break;
            case ("july"): MonthReturn = 7; break;
            case ("august"): MonthReturn = 8; break;
            case ("september"): MonthReturn = 9; break;
            case ("october"): MonthReturn = 10; break;
            case ("november"): MonthReturn = 11; break;
            case ("december"): MonthReturn = 12; break;
            default: MonthReturn = 0; break;
        }
        return MonthReturn;
    }
}
4

4 に答える 4

0

文字列 "1"、"2"、"3"、...、"12" を monthToNumber メソッドに追加できます。これにより、String.valueOf を使用してユーザー入力を整数に変換する必要がなくなります。

于 2013-11-13T17:31:41.937 に答える
0

ええ、number to month メソッドでは、月の名前を時系列で並べた String 配列を作成し、指定された入力値のインデックスから 1 を引いた位置にある文字列を取得するだけです。

于 2014-01-23T16:14:27.397 に答える
0

これはどう?

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Calendar;
import static java.util.Calendar.APRIL;
import static java.util.Calendar.AUGUST;
import static java.util.Calendar.DECEMBER;
import static java.util.Calendar.FEBRUARY;
import static java.util.Calendar.JANUARY;
import static java.util.Calendar.JULY;
import static java.util.Calendar.JUNE;
import static java.util.Calendar.MARCH;
import static java.util.Calendar.MAY;
import static java.util.Calendar.NOVEMBER;
import static java.util.Calendar.OCTOBER;
import static java.util.Calendar.SEPTEMBER;

public class Months {
public static void main(String []args) throws IOException {
    Integer iInput = null;
    String szInput = null;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please type either the number of the month, or the month itself to convert.");
    System.out.print("> ");
    szInput = br.readLine();

    boolean wasInt = false;
    try {
      iInput = Integer.valueOf(szInput);
      System.out.println(numberToMonth(iInput));
      wasInt = true;
    }
    catch(Exception e) {
    }

    if (! wasInt) {
        szInput = szInput.toLowerCase();
        System.out.println(monthToNumber(szInput));
    }
    return;
}

public static String numberToMonth(int iMonth) {
    switch(iMonth-1) {
        case (JANUARY): return "January"; 
        case (FEBRUARY): return "February"; 
        case (MARCH): return "March"; 
        case (APRIL): return "April"; 
        case (MAY): return "May"; 
        case (JUNE): return "June"; 
        case (JULY): return "July"; 
        case (AUGUST): return "August"; 
        case (SEPTEMBER): return "September"; 
        case (OCTOBER): return "October"; 
        case (NOVEMBER): return "November";
        case (DECEMBER): return "December";
    }
    return "Unknown";
}

public static int monthToNumber(String szMonth) {
  if (szMonth == null) {
    return 0;
  }
    switch(szMonth.toLowerCase()) {
        case ("january"): return 1 + JANUARY;
        case ("february"): return 1 + FEBRUARY;
        case ("march"): return 1 + MARCH;
        case ("april"): return 1 + APRIL;
        case ("may"): return 1 + MAY;
        case ("june"): return 1 + JUNE;
        case ("july"): return 1 + JULY;
        case ("august"): return 1 + AUGUST;
        case ("september"): return 1 + SEPTEMBER;
        case ("october"): return 1 + OCTOBER;
        case ("november"): return 1 + NOVEMBER;
        case ("december"): return 1 + DECEMBER;
    }
    return 0;
}
}
于 2013-11-13T17:35:34.793 に答える