创建一个描述学生的类Student,其中包括:
用于表示学生的姓名、性别、年龄和语、数、外3门课程成绩的数据成员;
要求包含1个可以给所有数据成员设定初始值的构造方法;
要求给语、数、外3门课程成绩分别定义设置方法和获取方法;
要求定义一个用于计算3门课程平均成绩的方法average()。
创建TestStudent类,包含main方法,在其中创建3个学生对象,比较他们的平均成绩,并输出平均成绩最高的那个学生的所有信息。
上面是题目,偶是新手,寒假在家里练习,如下是我写的代码。有一些错误及某些还没写完整,请教哈!
class Student {
String name;
String sex;
int age;
int chinese;
int english;
int maths;Student(int chinese,int english,int maths){
this.setChinese(c);
this.setEnglish(e);
this.setMaths(m);
}
public void setChinese(int c){
this.chinese=c;
}
public void setEnglishi(int e){
this.english=e;
}
public void setMaths(int m){
this.maths=m;
}
public int getChinese(){
return chinese;
}
public int getEnglish(){
return english;
}
public int getMaths(){
return maths;
}
public int average(int c,int e,int m){
return (c+e+m)/3;
}
}public class TestStudent{
public static void main(String[] args){
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.average(78,85,95);
s2.average(87,68,91);
s3.average(69,75,89);
System.out.println();
System.out.println();
}
}
用于表示学生的姓名、性别、年龄和语、数、外3门课程成绩的数据成员;
要求包含1个可以给所有数据成员设定初始值的构造方法;
要求给语、数、外3门课程成绩分别定义设置方法和获取方法;
要求定义一个用于计算3门课程平均成绩的方法average()。
创建TestStudent类,包含main方法,在其中创建3个学生对象,比较他们的平均成绩,并输出平均成绩最高的那个学生的所有信息。
上面是题目,偶是新手,寒假在家里练习,如下是我写的代码。有一些错误及某些还没写完整,请教哈!
class Student {
String name;
String sex;
int age;
int chinese;
int english;
int maths;Student(int chinese,int english,int maths){
this.setChinese(c);
this.setEnglish(e);
this.setMaths(m);
}
public void setChinese(int c){
this.chinese=c;
}
public void setEnglishi(int e){
this.english=e;
}
public void setMaths(int m){
this.maths=m;
}
public int getChinese(){
return chinese;
}
public int getEnglish(){
return english;
}
public int getMaths(){
return maths;
}
public int average(int c,int e,int m){
return (c+e+m)/3;
}
}public class TestStudent{
public static void main(String[] args){
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.average(78,85,95);
s2.average(87,68,91);
s3.average(69,75,89);
System.out.println();
System.out.println();
}
}
Student s1 = new Student(100, 80, 70);
Student s2 = new Student(70, 85, 66);
Student s3 = new Student(76, 100, 94); System.out.println("averages: " + s1.average() + ", " + s2.average() + ", " + s3.average());
}
}class Student { private int chinese; private int english; private int math; Student(int chinese, int english, int math) {
this.chinese = chinese;
this.english = english;
this.math = math;
} public double average() {
return (chinese + english + math) / 3.0;
} public int getChinese() {
return chinese;
} public void setChinese(int chinese) {
this.chinese = chinese;
} public int getEnglish() {
return english;
} public void setEnglish(int english) {
this.english = english;
} public int getMath() {
return math;
} public void setMath(int math) {
this.math = math;
}
}
class Student {
String name;
String sex;
int age;
double chinese; // 建议使用double,我觉得成绩可能是小数
double english;
double maths; Student(String name, String sex, int age, double chinese, double english, double maths) {
this.name = name;
this.sex = sex;
this.age = age;
this.chinese = chinese;
this.english = english;
this.maths = maths;
} public void setChinese(double c) {
this.chinese = c;
} public void setEnglish(double e) {
this.english = e;
} public void setMaths(double m) {
this.maths = m;
} public double getChinese() {
return chinese;
} public double getEnglish() {
return english;
} public double getMaths() {
return maths;
} public String getName() {
return name;
} public String getSex() {
return sex;
} public int getAge() {
return age;
} public double average() {
return (this.chinese + this.english + this.maths) / 3.0; // 必须是3.0,不能是3,否则就是整除了
}
}
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student("james", "male", 20, 89, 85, 90);
Student s2 = new Student("make", "female", 20, 75, 85, 90);
Student s3 = new Student("kobe", "male", 23, 90, 85, 95); Student max = s1;
if (s2.average() > max.average()) {
max = s2;
}
if (s3.average() > max.average()) {
max = s3;
} System.out.println("name=" + max.getName() + "\tsex=" + max.getSex() + "\tage=" + max.getAge()
+ "\tchinese=" + max.getChinese() + "\tenglish=" + max.getEnglish() + "\tmaths="
+ max.getMaths()); }
}
class Student {
String name;
String sex;
int age;
int chinese;
int english;
int maths; public Student(String name, String sex, int age, int chinese, int english,
int maths) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.chinese = chinese;
this.english = english;
this.maths = maths;
} public void setChinese(int c) {
this.chinese = c;
} public void setEnglishi(int e) {
this.english = e;
} public void setMaths(int m) {
this.maths = m;
} public int getChinese() {
return chinese;
} public int getEnglish() {
return english;
} public int getMaths() {
return maths;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public void setEnglish(int english) {
this.english = english;
} public int average() { return (chinese + english + maths) / 3;
}
}public class TestStudent {
// 比较大小
public static int compare(int a, int b, int c) {
int most = 0;
if (a >= b) {
if (a >= c) {
most = a;
} else {
most = c;
}
} else {
if (b >= c) {
most = b;
} else {
most = c;
}
}
return most;
} public static void main(String[] args) {
Student s1 = new Student("张三", "男", 25, 78, 85, 95);
Student s2 = new Student("李四", "男", 24, 87, 68, 91);
Student s3 = new Student("王五", "男", 23, 69, 75, 89); int a = s1.average();
int b = s2.average();
int c = s3.average();
int most = compare(a, b, c);
if (most == a) {
System.out.println("最高个人平均值为:"+most+"\n"+"以下为详细信息:");
System.out.println(s1.getName() + " " + s1.getSex() + " "
+ s1.getAge() + " 语文:" + s1.getChinese() + " 英语:"
+ s1.getEnglish() + " 数学:" + s1.getMaths());
}else if(most==b){
System.out.println("最高个人平均值为:"+most+"\n"+"以下为详细信息:");
System.out.println(s2.getName() + " " + s2.getSex() + " "
+ s2.getAge() + " 语文:" + s2.getChinese() + " 英语:"
+ s2.getEnglish() + " 数学:" + s2.getMaths());
}else {
System.out.println("最高个人平均值为:"+most+"\n"+"以下为详细信息:");
System.out.println(s3.getName() + " " + s3.getSex() + " "
+ s3.getAge() + " 语文:" + s3.getChinese() + " 英语:"
+ s3.getEnglish() + " 数学:" + s3.getMaths());
} }
}