鱼C论坛

 找回密码
 立即注册
查看: 4654|回复: 4

[已解决]明明定义Student了,Student cannot be resolved to a type

[复制链接]
发表于 2021-7-2 11:21:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. package com.atguigu.java;
  2. public class StudentTest {
  3.         public static void main(String[] args) {
  4.                
  5.                 Student[] stu = new Student[20] ;
  6.                 for(int i = 0; i < stu.length; i++) {
  7.                         stu[i].number = i + 1;
  8.                         stu[i].state = (int)(Math.random()*(6-1+1)+1);
  9.                         stu[i].score  = (int)(Math.random()*(100-0));
  10.                         if(stu[i] == 3) {
  11.                                 System.out.println(stu[i].info);
  12.                                
  13.                 }
  14.         }
  15.        
  16. class Student{
  17. int number,state,score;
  18. public String info() {
  19.                 return  state+"年级学生学号为:" + number + "成绩为:" + score+ "/t" ;
  20.         }

  21.                
  22.                 }
  23.         }
  24. }
  25.        
  26.                

  27.        


复制代码
最佳答案
2021-7-2 12:40:24
本帖最后由 wsw530 于 2021-7-2 12:52 编辑
甲鱼飞鱼 发表于 2021-7-2 11:23
第五行报错Student cannot be resolved to a type
  1. 错误地方有三个
  2. 1、java中的对象都需要用new 来生成,你这new Student[20],只是生成了数组,而没有创建一个个Student对象,需要在for循环第一行,实例化对象 stu[i] = new Student();
  3. 2、if(stu[i] == 3)这里stu[i]是一个对象不能呢个直接和数字3去比较,建议改为stu[i].number == 3
  4. 3、 System.out.println(stu[i].info);对象调用函数 对象.方法名(参数),建议改为System.out.println(stu[i].info());
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-2 11:23:23 | 显示全部楼层
第五行报错Student cannot be resolved to a type
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-2 12:40:24 | 显示全部楼层    本楼为最佳答案   
本帖最后由 wsw530 于 2021-7-2 12:52 编辑
甲鱼飞鱼 发表于 2021-7-2 11:23
第五行报错Student cannot be resolved to a type
  1. 错误地方有三个
  2. 1、java中的对象都需要用new 来生成,你这new Student[20],只是生成了数组,而没有创建一个个Student对象,需要在for循环第一行,实例化对象 stu[i] = new Student();
  3. 2、if(stu[i] == 3)这里stu[i]是一个对象不能呢个直接和数字3去比较,建议改为stu[i].number == 3
  4. 3、 System.out.println(stu[i].info);对象调用函数 对象.方法名(参数),建议改为System.out.println(stu[i].info());
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-2 12:53:02 | 显示全部楼层
wsw530 发表于 2021-7-2 12:40
错误地方有三个
1、java中的对象都需要用new 来生成,你这new Student[20],只是生成了数组,而没有创建 ...

改完了还是报错Student cannot be resolved to a type
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-2 13:09:33 | 显示全部楼层
甲鱼飞鱼 发表于 2021-7-2 12:53
改完了还是报错Student cannot be resolved to a type

改正确了,非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表