为什么调不到state方法,


public class ExerTest3{
    public static void main(String[] args) {
        Student8[] test = new Student8[20];
         for(int i = 0;i < test.length;i++) {
             test[i] = new Student8();
             test[i].number = i+1;
             test[i].state = (int)(Math.random()*6+1);
             test[i].score = (int)(Math.random()*101+1);
         }
             test[i].state(test, 3);
      } 
   }
 
class Student8 {
    int number,state,score;
    /**
     * 
     * @Description  输出学生信息
     * @author xiong
     * @date 2022年3月29日上午11:17:12
     * @param test
     */
    public void info(Student8[] test ) {
        System.out.println("学生学号:" + number + "  学生年级:" + state
                + " 学生成绩: " + score);
    }
    /**
     * 
     * @Description  找出相同年级的学生
     * @author xiong
     * @date 2022年3月29日上午11:16:27
     * @param test  
     * @param state
     */
    public void state(Student8[] test,int state) {
        for(int i = 0;i < test.length;i++) {
             if(test[i].state == state) {
                 test[i].info(test);
             }
        
        }
    }
}
 
public class ExerTest3{
    public static void main(String[] args) {
        Student8[] test = new Student8[20];
         for(int i = 0;i < test.length;i++) {
             test[i] = new Student8();
             test[i].number = i+1;
             test[i].state = (int)(Math.random()*6+1);
             test[i].score = (int)(Math.random()*101+1);
             test[i].state(test, 3);
         }
      } 
   }

img

你写到for循环外面去了

11行的test[i].state(test, 3);你写到了for循环的外面了


public static void main(String[] args) {
        Student8[] test = new Student8[20];
         for(int i = 0;i < test.length;i++) {
             test[i] = new Student8();
             test[i].number = i+1;
             test[i].state = (int)(Math.random()*6+1);
             test[i].score = (int)(Math.random()*101+1);
             test[i].state(test, 3);
         }
             
      } 
   }

不是state()不能调用,而是test[i]无法访问,因为test[i]在for循环外,i是访问不到的

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632