【发布时间】:2023-03-31 23:08:01
【问题描述】:
在范围界定方面存在一些问题。我正在尝试使用一个循环编写一个程序,该循环从键盘获取 10 个代表考试成绩(0 到 100 之间)的值,并输出所有输入值的最小值、最大值和平均值。我的程序不能接受小于 0 或大于 100 的值。
import java.util.Scanner;
import java.util.Arrays;
public class ExamBookClient
{
public static void main( String[] args)
{
Scanner scan = new Scanner(System.in);
int MAX = 100;
int MIN = 0;
int[] grades = new int[10];
System.out.println("Please enter the grades into the gradebook.");
if(scan.hasNextInt())
{
for (int i = 0; i < grades.length; i++)
{
if( x>MIN && x<MAX)
{
int x = scan.nextInt();
grades[i] = x;
}
}
}
System.out.print("The grades are " + grades.length);
}
}
我的编译器错误是我无法修复范围错误:
ExamBookClient.java:21: error: cannot find symbol
if( x>MIN && x<MAX)
^
symbol: variable x
location: class ExamBookClient
ExamBookClient.java:21: error: cannot find symbol
if( x>MIN && x<MAX)
^
【问题讨论】:
-
在 x 范围内之前不要使用它。提前声明。