0

我最初试图让我的 throw 语句在没有 try catch 的情况下工作,并且该userInput = input.nextInt();行工作正常。但是当我尝试添加 try..catch 时,它不喜欢我的输入,说它无法解决。我不认为我的 try..catch 是正确的,但我计划在我可以让这个输入被识别之后解决这个问题,但我也很感激你看到的任何反馈。

谢谢

import java.util.Scanner;

    public class Program6 
    {
        public static void main(String[] args) 
        {
            final int NUMBER_HIGH_LIMIT = 100;
            final int NUMBER_LOW_LIMIT = 10;
            int userInput;

            try
            {
                System.out.print("Enter a number between 10 and 100: ");
                userInput = input.nextInt();//Says input cannot be resolved

                Verify v = new Verify(NUMBER_HIGH_LIMIT, NUMBER_LOW_LIMIT);
            }
            catch(NumberHighException exception)
            {
                userInput = 0;
            }
            catch(NumberLowException exception) 
            {
                userInput = 0;  
            }
        }
    }
4

1 回答 1

4

您需要创建一个名为input

public class Program6 {

  public static void main(String[] args) {
    final int NUMBER_HIGH_LIMIT = 100;
    final int NUMBER_LOW_LIMIT = 10;
    int userInput;

    try {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a number between 10 and 100: ");
      userInput = input.nextInt();//Says input cannot be resolved

      Verify v = new Verify(NUMBER_HIGH_LIMIT, NUMBER_LOW_LIMIT);

    } catch (NumberHighException exception) {
      userInput = 0;
    } catch (NumberLowException exception) {
      userInput = 0;
    }
  }
}
于 2013-10-17T21:50:29.747 回答