|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- package com.atguigu.java;
- public class StudentTest {
- public static void main(String[] args) {
-
- Student[] stu = new Student[20] ;
- for(int i = 0; i < stu.length; i++) {
- stu[i].number = i + 1;
- stu[i].state = (int)(Math.random()*(6-1+1)+1);
- stu[i].score = (int)(Math.random()*(100-0));
- if(stu[i] == 3) {
- System.out.println(stu[i].info);
-
- }
- }
-
- class Student{
- int number,state,score;
- public String info() {
- return state+"年级学生学号为:" + number + "成绩为:" + score+ "/t" ;
- }
-
- }
- }
- }
-
-
-
复制代码
本帖最后由 wsw530 于 2021-7-2 12:52 编辑
- 错误地方有三个
- 1、java中的对象都需要用new 来生成,你这new Student[20],只是生成了数组,而没有创建一个个Student对象,需要在for循环第一行,实例化对象 stu[i] = new Student();
- 2、if(stu[i] == 3)这里stu[i]是一个对象不能呢个直接和数字3去比较,建议改为stu[i].number == 3
- 3、 System.out.println(stu[i].info);对象调用函数 对象.方法名(参数),建议改为System.out.println(stu[i].info());
复制代码
|
|