有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何修复这个简单的作用域错误?

在范围界定方面有一些问题。我试图用一个循环编写一个程序,从键盘上取代表考试成绩的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)
                          ^

共 (1) 个答案

  1. # 1 楼答案

    在if上移动x

    if(scan.hasNextInt())
       {
         for (int i = 0; i < grades.length; i++)
          {
             int x = scan.nextInt();
             if( x>MIN && x<MAX)
             {
    
             grades[i] = x;
          }
       }