-1

それで、Googleとstackoverflowを少し検索しましたが、この問題に対する答えが見つからないようです。

私は2つの方法を持っています。最初のメソッドである getAge は、ユーザーからの入力として整数を取得するためのものです。次に、その入力を verifyAge に渡します。verifyAge は、それが正しい範囲にあることを確認します。

でも; 整数以外を入力する必要がある場合は、メッセージを表示して getAge を再度呼び出し、入力プロセスを再開する必要があります。try-catch をセットアップしましたが、それでも JVM に戻ります。別の投稿の回答によると; 私がしていることは正しいです。しかし、まだ機能していないようです。したがって、現在の状態で実行しようとすると、次のエラーが表示されます。

Please enter your age: notint
Exception in thread "main" java.util.InputMismatchException
 at java.util.Scanner.throwFor(Scanner.java:864)
 at java.util.Scanner.next(Scanner.java:1485)
 at java.util.Scanner.nextInt(Scanner.java:2117)
 at java.util.Scanner.nextInt(Scanner.java:2076)
 at Ch2ProgLabWilson.getAge(Ch2ProgLabWilson.java:22)
 at Ch2ProgLabWilson.main(Ch2ProgLabWilson.java:15)

私が書いたこと:

import java.util.* ;
import java.util.Scanner;

public class Ch2ProgLabWilson {

 public static void main(String[] args) {

      getAge();  
 }

 public static int getAge()
 {
  Scanner keyboard = new Scanner(System.in);
  System.out.print("Please enter your age: ");
  int a = keyboard.nextInt();
  verifyAge(a);

     try
     {
        getAge();
     }    
     catch (InputMismatchException e)
        {
           System.out.println("You may only enter integers as an age. Try again.");
           getAge();
        }

    return a;
   }

 // 
 public static boolean verifyAge (int a)
     {
        if (a >= 0 && a <= 122)
        {
           System.out.println("The age you entered, " + a + ", is valid.");
           return true;
        }
        else
        {
           System.out.println("The age must be from 0 to 122, cannot be negative, and has to be an integer.");
           getAge();
           return false;
        }   
     }

 }
4

3 に答える 3

3

例外はint a = keyboard.nextInt();、try catch ブロックの外側にある によってスローされています。int a = keyboard.nextInt();try ブロック内に呼び出しを配置し​​ます。

コードには他にも問題があります。

verifyAge()使用されていないa を返しますboolean

あなたのgetAge()メソッドは再帰的であり、ユーザーが数字を入力すると仮定すると、ループするだけです-これはあなたが意図したものですか?

アップデート

public static int getAge(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter your age: ");
    int age = -1;

    while(!verifyAge(age)){ // will loop until there's a valid age
        try{
            age = scanner.nextInt();
        catch (InputMismatchException e){
            System.out.println("You may only enter integers as an age. Try again.");
        }
    }

    return age; // your current code doesn't do anything with this return value
}

public static boolean verifyAge (int a){ // would be better named isValidAge()
    if (a >= 0 && a <= 122){
        System.out.println("The age you entered, " + a + ", is valid.");
        return true;
    }else{ // no need to call getAge() here
       System.out.println("The age must be from 0 to 122, cannot be negative, and has to be an integer.");
       return false;
    }   
 }
于 2014-09-08T07:53:13.393 に答える
2

コードはtryブロック内にありません

  Scanner keyboard = new Scanner(System.in);
  System.out.print("Please enter your age: ");
  int a = keyboard.nextInt();
  verifyAge(a);

  try
  {

だから引っかからない。

于 2014-09-08T07:53:16.767 に答える
1

実際に例外をスローするコード行を見てください。ブロックに入ってtry/catchいないと引っかかりません。

代わりにこれを試してください:

try
{
    int a = keyboard.nextInt();
    verifyAge(a);
}    
catch (InputMismatchException e)
{
    System.out.println("You may only enter integers as an age. Try again.");
    getAge();
}
于 2014-09-08T07:53:33.577 に答える