-2

「1500、1と1」または「1500、1月と1」のような3つの入力を取り、「1500年1月1日」または「1/1/1500」を返すものを作成しています。その日にいくつかの問題がありました一部ですが、誰かがすでにそれを修正する方法を教えてくれました。そして今、月の部分に問題があります。これをちょっと速くしましたが、なぜ機能しないのかわかりません。入力が有効な月である場合は月を出力します (この部分はテスト用です)。それはただ停止するだけで何も出力しない月ではありません。何もしないだけで月を置いても、エラーがあるかどうかを確認しようとしましたが、何も見つかりませんでした。私が使用したコードです:

   Scanner scan = new Scanner(System.in);
   String mx;
   System.out.println("Insert Month");
   String[] mm = {"january","february","march","april","may","june","july","august","september","october","november","december"};
   int mz = 0;
   while (0 < 1){
   mx = scan.nextLine();
       mx = mx.toLowerCase();
       for(int i = 0; i < 11; i++){
           if (mx.equals(mm[i])){
               mz = i + 1;
               break;
           }
           else {
               if(i == 11){
                   System.out.println("please use a valid month or a number between 1 and 12");
               }
               else{
               }
           }
       } 
   //}
   if(mz > 0){
       break;
   }
   else {}
   }
   System.out.println(mx);
4

2 に答える 2

0

意味のある変数名を使用していないため、コードの読み取りと保守が少し難しくなっています。そのため、次のコードを最初から作成する必要がありました。

public static void main(String[] args)
{
        Scanner keyboard = new Scanner(System.in);

        String month = getMonthName(getInt("Enter Month: ", keyboard) - 1);
        int day = getInt("Enter Day: ", keyboard);
        int year = getInt("Enter Year: ", keyboard);

        System.out.printf("%s %d, %d\n", month, day, year);

}

public static String getMonthName(final int monthNo)
{
       String[] months = {"january","february","march","april","may","june","july","august","september","october","november","december"};
       return months[monthNo];
} 


public static int getInt(final String msg, Scanner keyboard)
{
        System.out.print(msg);
        return keyboard.nextInt();
}

すでにお気づきかもしれませんが、上記のコードは入力検証を実行しません。たとえば、月の入力を検証する場合、if 条件は次のようになります。

if (month < 0 || month < 12)
{
 System.out.println("Invalid month number entered");      
 System.exit(0);
}
于 2013-07-26T19:41:43.507 に答える
0

i == 11プログラムが単に「停止」する理由は、「有効な月を入力してください...」というステートメントのみを出力し、forif ループブレークがあるためですi >= 11。したがって、この条件が満たされることはありません。このステートメントは出力されませんが、while ループは実行され続けます。最初の試行で月以外の文字列を入力し、2 回目に月の文字列を入力すると、while ループが壊れてしまう可能性があります。

これが、月に取得するために機能するようにコードを改善した方法です。ハイライトの微妙な変化に注目。これらは、より適切で読みやすいコードを書くために重要です。

Scanner scan = new Scanner(System.in);
//initialize to empty string
String mx = "";
System.out.println("Insert Month");
//use good naming conventions for easier code readability
String[] validMonths = {"january","february","march","april","may","june","july","august","september","october","november","december"};
//using a boolean to break makes much more sense than the way you have written it with an infinite loop and a manual break statement
boolean noMonth = true;
while (noMonth){
    mx = scan.nextLine();
    for(int i = 0; i < 12; i++){
        //rather than convert to lowercase, use this handy String method
        //also compares for valid number entries
        if (mx.equalsIgnoreCase(validMonths[i]) || mx.equals(Integer.toString(i+1))){
             noMonth = false;
             break;
        }
    }
    if(noMonth){
        System.out.println("please use a valid month or a number between 1 and 12");
    }
}
System.out.println(mx);

その日を取り込む新しい while ループと、これらの翌年を取り込む新しい while ループを作成し、有効な入力をチェックします。また、Java では、すべての if に else は必要ありません。

于 2013-07-26T19:43:32.753 に答える