小强GX 2021-07-14 17:48 采纳率: 100%
浏览 59
已采纳

下面图片中圈红的地方std[i] = new Student();为什么会报错呢?

完整代码:

public class  {
    public static void main(String[] args) {
    //创建20个学生对象:    
        Student[] std = new Student[20];
        for(int i = 0;i < std.length;i++) {
            std[i] = new Student();
            //学号为1~20:    
            std[i].number = (i+1);
            //年级Math.random[0.0 1.0)*6-->[0.0 6.0)-->(int)[0 6)+1-->[1 6]
            std[i].state = (int)(Math.random()*6) + 1;
            //成绩[0 100]
            std[i].score = (int)(Math.random()*100) + 1;
        }
    
    }
    

//定义类Student:
class Student{
//包含三个属性:    
    int number;//学号
    int state;//年级
    int score;//成绩
    
    //遍历学生属性信息方法:
    public String info() {
        return "学号:"+number+"\t"+"年级:"+state+"\t"+"成绩:"+score;
        }
    
    //遍历20个学生的属性信息:    
        public void arrayergo(Student[] std) {
            for(int i = 0;i < std.length;i++){
                    System.out.println(std[i].info());    
            }    
        }
    
    //问题一:打印出3年级(state值为3)的学生信息:
        public void statefind(Student[] std,int state) {    
            for(int i = 0;i < std.length;i++) {
                if(std[i].state==state) {
                    System.out.println(std[i].info());
                }
            }
        }
    }
}

图片:

![img](

https://img-mid.csdnimg.cn/release/static/image/mid/ask/939358552626159.png)

  • 写回答

4条回答 默认 最新

  • 林林林林七 2021-07-14 17:53
    关注

    img
    main方法是静态 你的内部类不是静态

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已采纳回答 7月14日
  • 创建了问题 7月14日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度