-1

所以,我在google和stackoverflow上搜索了一些,但我似乎找不到这个问题的答案。

我有两种方法。第一种方法 getAge 只是从用户那里获取一个整数作为输入。然后它意味着将该输入传递给 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()返回一个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 回答