一个java问题,希望高手改错并有错误解答... ... 问题如下:

2024-12-19 11:11:45
推荐回答(5个)
回答1:

你的程序,我帮你改完了,运行没问题了。
class Student
{
String name;
int age;
String sex;
static String schoolName;
static int sum=0;
Student(String name,int age,String sex)
{
this.name=name;
this.age=age;
this.sex=sex;
sum++;
}
String getName()
{
return name;
}
void setName(String s)
{
name=s;
}
void setAge(int s)
{
age=s;
}
int getAge()
{
return age;
}
static void setschoolName(String s)
{
schoolName=s;
}
void outInfo()
{
System.out.println(name+"同学是"+sex+"生,"+"今年"+age+"岁");
}
}
class GraduateStudent extends Student
{
String speciality;
String teacher;
GraduateStudent(String name,int age,String sex,String speciality,String teacher)
{
super(name,age,sex);
this.speciality=speciality;
this.teacher=teacher;
}
void outInfo()
{
System.out.println(name+"同学是"+sex+"生,"+"今年"+age+"岁,"+"该学生专业为:"+speciality+",导师是"+teacher);
}
}

class studentTest
{
public static void main(String[] args)
{
Student rg1[]=new Student[4];
String name[]={"杨娜","葛根","芬妮","晴空"};
String sex[]={"女","男","女","女"};
int age[]={16,19,18,16};
for(int i=0;i<4;i++)
{
rg1[i]=new Student("",0,"");
rg1[i].name=name[i];
rg1[i].sex=sex[i];
rg1[i].age=age[i];
}
for(int i=0;i<=3;i++)
{
rg1[i].age=age[i]+1;
if(rg1[i].age<20)
{
rg1[i].outInfo();
}
}

GraduateStudent yjs[]=new GraduateStudent[2];
String na[]={"东海","布克"};
String xb[]={"男","男"};
int nl[]={23,22};
String speciality[]={"软件工程","经济管理"};
String teacher[]={"李云博","王坤坤"};
for(int i=0;i<2;i++)
{
yjs[i]=new GraduateStudent("",0,"","","");
yjs[i].name=na[i];
yjs[i].sex=xb[i];
yjs[i].age=nl[i];
yjs[i].speciality=speciality[i];
yjs[i].teacher=teacher[i];
yjs[i].outInfo();
}
Student.setschoolName("宝鸡文理学院");
System.out.println(Student.schoolName+"有"+Student.sum+"个学生");
}
}

回答2:

class Student
{
String name;
int age;
String sex;
static String schoolName;
static int sum=0;
Student(String name,int age,String sex)
{
this.name=name;
this.age=age;
this.sex=sex;
sum++;
}
void setName(String s)
{
name=s;
}
void setAge(int s)
{
age=s;
}
int getAge()
{
return age;
}
static void setschoolName(String s)
{
schoolName=s;
}
void outInfo()
{
System.out.println(name+"同学是"+sex+"生,"+"今年"+age+"岁");
}
}
class GraduateStudent extends Student
{
String speciality;
String teacher;
GraduateStudent(String name,int age,String sex,String speciality,String teacher)
{
super(name,age,sex);
this.speciality=speciality;
this.teacher=teacher;
}
void outInfo()
{
System.out.println(name+"同学是"+sex+"生,"+"今年"+age+"岁,"+"该学生专业为:"+speciality+",导师是"+teacher);
}
}

class studentTest
{
public static void main(String[] args)
{
Student rg1[] = new Student[4];
String name[]={"杨娜","葛根","芬妮","晴空"};
String sex[]={"女","男","女","女"};
int age[]={16,19,18,16};
for(int i=0;i<4;i++)
{
rg1[i]=new Student("",0,"");
rg1[i].name=name[i];
rg1[i].sex=sex[i];
rg1[i].age=age[i];
}
for(int i=1;i<4;i++)
{
rg1[i].age=rg1[i].age+1;
if(rg1[i].age<20)
{
rg1[i].outInfo();
}
}

GraduateStudent yjs[] = new GraduateStudent[2];
String na[]={"东海","布克"};
String xb[]={"男","男"};
int nl[]={23,22};
String speciality[]={"软件工程","经济管理"};
String teacher[]={"李云博","王坤坤"};
for(int i=0;i<2;i++)
{
yjs[i] = new GraduateStudent("", 0, "", "", "");
yjs[i].name=na[i];
yjs[i].sex=xb[i];
yjs[i].age=nl[i];
yjs[i].speciality=speciality[i];
yjs[i].teacher=teacher[i];
yjs[i].outInfo();
}
Student.setschoolName("宝鸡文理学院");
System.out.println(Student.schoolName+"有"+Student.sum+"个学生");
}
}
上面是修改之后的程序
你的问题主要是数组的定义没对,好好对照下 不懂再问我哇~~

回答3:

你的错误实在太多了,跟你指出一些,自己改吧,还是要多练练啊,
1、在第一个类里面:
void setAge(int s)
{
name=s;
}
name是String类型,你不能把一个整形的s赋给name的;
2、所有的数组定义全错了:
Student rg1[4];
String name[4]={"杨娜","葛根","芬妮","晴空"};
String sex[4]={"女","男","女","女"};
int age[4]={16,19,18,16};
若不赋值,Student []rg1=new Student[4];
赋值的话在[]里面不要写数字:
String name[]={"杨娜","葛根","芬妮","晴空"};
String sex[]={"女","男","女","女"};
int age[]={16,19,18,16};

回答4:

myVote=new Votes(1,"Julia");
将此时创建的Votes对象赋给类Votes的对象myVote,并且通过构造函数给该对象的voteNumber和 candidateName属性赋值
而此时该对象的next属性为null;
输出toString结果为 1. Julia "\n"
myVote=myVote.addVote(2,"Tony");
将此时myVote的addVote方法返回的Votes对象赋给类Votes的对象myVote(注意myVote是全局变量),现在的myVote已经变成新的对象了,而方法addVote内部调用了setNaxt()方法(你没写),这样就设置了当前myVote的next属性,并且值为this(调用该方法时的当前对象即老的myVote)
故输出toString结果为:
2.Tony
1.Julia

虽然setNext()方法没写但我觉得这程序应该是这个意思,希望对你有帮助。

回答5:

String na[2]={"东海","布克"};
String xb[2]={"男","男"};
int nl[2]={23,22};
String speciality[2]={"软件工程","经济管理"};
String teacher[2]={"李云博","王坤坤"};
这种语法看起来受C的影响比较多,可惜在JAVA里是错误的
对数组进行初始化,可参考下面的语句:
String[] xb = new String[]{"男", "男"};
如果只做定义,不初始化,则必须在等号右边指定数组大小:
String[] xb = new String[2];
对已有的数组元素赋值:
String[] xb = new String[2];
xb[0] = "aaa"