一个java问题,希望高手改错并有错误解答... ... 问题如下:
答案:5 悬赏:0 手机版
解决时间 2021-02-13 01:38
- 提问者网友:箛茗
- 2021-02-12 08:31
一个java问题,希望高手改错并有错误解答... ... 问题如下:
最佳答案
- 五星知识达人网友:冷風如刀
- 2021-02-12 09:25
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+"个学生");
}
}
上面是修改之后的程序
你的问题主要是数组的定义没对,好好对照下 不懂再问我哇~~追问是不是不在创建的时候赋值,数组括号里就可以写数字,要是赋值的话,就是空括号?追答在中括号里写数字和大括号里直接赋值是数组的两种创建形式,这两种形式可以选择其中一种,但你不能既在中括号里写数字,又在大括号赋值,还要注意的是如果你想要用大括号给数组赋值,那么你只能在数组声明的时候使用大括号赋值,简单的举个例子:
比如你可以这样:String s[] = {"1","2"};
但是你不能这样:
String s[];
s = {"1","2"};
而在总括号里写数字的方式初始化则不受此限制:
比如你可以这样:String s[] = new String[2];
也可以这样:
String s[];
s = new String[2];
{
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+"个学生");
}
}
上面是修改之后的程序
你的问题主要是数组的定义没对,好好对照下 不懂再问我哇~~追问是不是不在创建的时候赋值,数组括号里就可以写数字,要是赋值的话,就是空括号?追答在中括号里写数字和大括号里直接赋值是数组的两种创建形式,这两种形式可以选择其中一种,但你不能既在中括号里写数字,又在大括号赋值,还要注意的是如果你想要用大括号给数组赋值,那么你只能在数组声明的时候使用大括号赋值,简单的举个例子:
比如你可以这样:String s[] = {"1","2"};
但是你不能这样:
String s[];
s = {"1","2"};
而在总括号里写数字的方式初始化则不受此限制:
比如你可以这样:String s[] = new String[2];
也可以这样:
String s[];
s = new String[2];
全部回答
- 1楼网友:街头电车
- 2021-02-12 14:29
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()方法没写但我觉得这程序应该是这个意思,希望对你有帮助。
将此时创建的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()方法没写但我觉得这程序应该是这个意思,希望对你有帮助。
- 2楼网友:蕴藏春秋
- 2021-02-12 13:44
你的程序,我帮你改完了,运行没问题了。
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+"个学生");
}
}
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+"个学生");
}
}
- 3楼网友:时间的尘埃
- 2021-02-12 12:09
你的错误实在太多了,跟你指出一些,自己改吧,还是要多练练啊,
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};
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楼网友:醉吻情书
- 2021-02-12 10:45
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"
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"
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯