gpt4 book ai didi

java - 添加 try..catch 时为 "input cannot be resolved"

转载 作者:行者123 更新时间:2023-12-01 18:42:20 26 4
gpt4 key购买 nike

我最初尝试让我的 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;
}
}
}

最佳答案

您需要创建一个名为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;
}
}
}

关于java - 添加 try..catch 时为 "input cannot be resolved",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19437918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com