谁能帮我看看这个java程序有什么问题呀~~?

来源:百度知道 编辑:UC知道 时间:2024/06/22 15:40:55
import java.util.*;
public class test4s {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("请输入这个人的身份");
String q=sc.next();
if(q.equals("班长")){
System.out.print("他的得分难道是>");
int score=sc.nextInt();
Student s=new StudentBZ("王小明",1,s.getScore(score),"班长");
}
System.out.print("他的得分难道是>");
int score=sc.nextInt();
if(q.equals("学猥")){
Student s=new StudentXW("韦阿宝",2,s.getScore(score),"学猥");
}
if(q.equals("学员")){
Student k[]=new Student[3];
for(int j=0;j<3;j++){
int num=3;
num++;
int id=num;
System.out.print("他的得分难道是>");
int score=sc.nextInt();
System.out.print("他就是

因为你在Student s=new Student("王小明",1,s.getScore(score),"班长");
时,这个s对象还没构造出来的时候就调用s.getScore(score) ,这时的s并没有构造出来,调用时出错,s没初始化。既然有算法,那很简单
在你的Student类的构造器写为:
public Student(String name,int id,int score,String zhiwei)
{
this.name = name ;
..
this.score = score+3;
}
那个get()方法就不要了,
在测试程序中
Student s=new StudentBZ("王小明",1,score,"班长");

Student s=new StudentBZ("王小明",1,score,"班长");