1

だから私はユーザーに月と年を求めています。month は 12 か月のいずれかである必要があり、year は数字である必要があり、文字は使用できません。私は、プログラムに「間違った入力、やり直してください」と言って、再度入力を促す最良の方法を見つけようとしています。これは、月のセクションで使用しているコードのセクションです。

public class MonthLength {
  public static void main(String[] args) {

     int month = 0;
    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();

      if (userInput.trim().toLowerCase().equals("january")) {
        month = 1;
      } else if (userInput.trim().toLowerCase().equals("february")) {
        month = 2;
      } else if (userInput.trim().toLowerCase().equals("march")) {
        month = 3;
      } else if (userInput.trim().toLowerCase().equals("april")) {
        month = 4;
      } else if (userInput.trim().toLowerCase().equals("may")) {
        month = 5;
      } else if (userInput.trim().toLowerCase().equals("june")) {
        month = 6;
      } else if (userInput.trim().toLowerCase().equals("july")) {
        month = 7;
      } else if (userInput.trim().toLowerCase().equals("august")) {
        month = 8;
      } else if (userInput.trim().toLowerCase().equals("september")) {
        month = 9;
      } else if (userInput.trim().toLowerCase().equals("october")) {
        month = 10;
      } else if (userInput.trim().toLowerCase().equals("november")) {
        month = 11;
      } else if (userInput.trim().toLowerCase().equals("december")) {
        month = 12;
      }


    // Terminate program if month is not a proper month name
    if (month < 1 || month > 12) {
      System.out.println("Illegal month name; try again");
      return;
    }

そして、これが私が年セクションで取り組んでいるものです:

    // Prompt the user to enter a year
    SimpleIO.prompt("Enter a year: ");
    userInput = SimpleIO.readLine();
    int year = Integer.parseInt(userInput);

    //Here, trying to use hasNextInt to make sure input is an integer
    //If it's not, need to give an error message and prompt input again
    // public boolean hasNextInt()

    //Prompt input again if year is negative
    if (year < 0) {
       System.out.println("Year cannot be negative; try again");
       return;
    }

    // Determine the number of days in the month
    int numberOfDays;
    switch (month) {
      case 2:  // February
               numberOfDays = 28;
               if (year % 4 == 0) {
                 numberOfDays = 29;
                 if (year % 100 == 0 && year % 400 != 0)
                   numberOfDays = 28;
               }
               break;

      case 4:  // April
      case 6:  // June
      case 9:  // September
      case 11: // November
               numberOfDays = 30;
               break;

      default: numberOfDays = 31;
               break;
    }

    // Display the number of days in the month
    System.out.println("There are " + numberOfDays +
                       " days in this month");
  }
}

コードを見た後、私が求めていることがより明確になると確信しています。月以外の単語を入力すると、プロンプトが表示され、再度入力を求められます。整数ではない年を入力した場合も同じです。前もって感謝します!

4

2 に答える 2

5

ループで実行すると、次のようになります。

String userInput;
int month;
do{
    SimpleIO.prompt("Enter a month name: ");
    userInput = SimpleIO.readLine();
    try{
        month = Integer.parseInt(userInput);
    } catch(NumberFormatException e){
        continue;
    }
}while(month <= 0 || month > 12);
于 2012-11-04T03:03:53.713 に答える
1

月が正しく挿入されるまでユーザーにプロンプ​​トを表示し続けるループを作成する必要があります。次の行の何か:

boolean correct_month = false; // Control variable 

while(!correct_month)
{
   int month = 0;
    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();

     ...
   // If the month is indeed correct
   // then correct_month = true;
}

次に、同じ考えを年に適用します。

月にこれらの条件をすべて持つのではなく、すべての月の文字列を ArrayList に追加する方がよいと思います。

ArrayList <String> all_months = new ArrayList <String> ();

その後all_months.indexOf、ユーザーが文字列を挿入して使用するだけです。-1 を返す場合、文字列は有効な月ではありません。それ以外の場合は、その月がリストにある位置を示します。例えば

month = all_months.indexOf(userInput);
if(month != -1){
   correct_month = true;
}

したがって、完全なソリューションは次のようになります。

ArrayList <String> all_months = new ArrayList <String> ();
all_months.add("january");
... // and so one

int month = 0; // Control variable 

while(month <= 0)
{

    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();

    month = all_months.indexOf(userInput.trim().toLowerCase()) + 1;
}
于 2012-11-04T03:02:55.790 に答える