我收到一条错误消息,说局部变量可能尚未初始化,或者无法解析为 catch 所在的变量。我应该如何解决这个问题?基本上,我希望我的程序接受一些数字,然后停止并显示一些答案。
import java.util.Scanner;
public class math2{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Integer Values\nEnter a Non-Integer When Finished\n");
int x = input.nextInt();
int max = x;
int min = x;
int sum = x;
double average = 0;
try
{
int amount = 1;
while(true)
{
x = Integer.parseInt(input.next());
sum = sum + x;
amount++;
average = (double)sum/amount;
if (x > max) {
max = x;
} else if (x < min) {
min = x;
}
}
}catch(NumberFormatException e){
System.out.println("The Maximum Value is " + max);
System.out.println("The Minimum Value Is " + min);
System.out.println("The Sum Is " + sum);
System.out.println("The Average Is " + average);}
}
}