周而复始3
2017-03-13 14:25
学校作业:
编写Java应用程序,封装一个Student类的对象。其中定义一个表示学生的类Student,包括学号、班级、性别、年龄属性,以及获得学号、获得性别、获得性别、获得姓名、获得年龄、修改年龄方法。另加一个public String toString()方法把Student类对象的所有信息组成一个字符串,并有检测这个功能的程序体现。
使用上题编写的Student类创建5个学生对象,并在控制台把5个学生的信息显示出来。
我的代码:
package homework;
public class Student {
String name;
int id;
String ClassNum;
String sex;
int age;
public Student(String name,int id,String ClassNum,String sex,int age){
this.name=name;
this.id=id;
this.ClassNum=ClassNum;
this.sex=sex;
this.age=age;
}
public String getName(){
return this.name;
}
public int getId(){
return this.id;
}
public String getClassNum(){
return this.ClassNum;
}
public String getSex(){
return this.sex;
}
public int getAge(){
return this.age;
}
public void setAge(int age){
this.age=age;
}
public String toString(){
return "My name is "+name+".My id is"+id+".My ClassNum is"+ClassNum+".My sex is"+sex+".My age is"+age+".";
}
Student a=new Student("ZhangSan",123,"class1","boy",11);
Student b=new Student("LiSi",1234,"class1","boy",11);
Student c=new Student("WangWu",12345,"class2","boy",11);
Student d=new Student("MingMing",54321,"class2","girl",12);
Student e=new Student("XingXing",4321,"class3","girl",12);
Student f=new Student("QingQing",321,"class3","girl",12);
System.out.priltln(a);
System.out.priltln(b);
System.out.priltln(c);
System.out.priltln(d);
System.out.priltln(e);
System.out.priltln(f);
}
画下划线是显示错误的地方。
请问要如何显示5个学生的信息???
找到原因了,创建对象和打印对象那几行代码应该写在方法里,比如main(),而不能直接写在类中
代码都是对的 但是你看我下面测试的不行
这两个问题
package homework;
public class Calculator {
int a;
int b;
double result;
public double sum(){
double sum=a+b;
return result=sum;
}
public double sub(){
double sub=a-b;
return result=sub;
}
public double mult(){
double mult=a*b;
return result=mult;
}
public void div(){
if(b==0){
System.out.println("b!=0");
}else{
double div=a/b;
System.out.println("result="+div);
}
}
public Calculator(int a,int b){
}
}
package homework;
public class CalculatorText {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator Calculator1 =new Calculator(3,4);
System.out.println(Calculator1.sum());
System.out.println(Calculator1.sub());
System.out.println(Calculator1.mult());
Calculator1.div();
Calculator Calculator2 =new Calculator(2,1);
System.out.println(Calculator2.sum());
System.out.println(Calculator2.sub());
System.out.println(Calculator2.mult());
Calculator2.div();
}
}
这个图
就是这个情况
方法名拼错了,不是priltln, 是println
Java入门第二季 升级版
530553 学习 · 6091 问题
相似问题