JAVA:定义一个表示学生信息的类student

2024年11月27日 20:24
有2个网友回答
网友(1):

1.Bean
package com.test;

public class Student {
private String sSO;//学号
private String sName;//姓名
private String sSex;//性别
private String sAge;//年龄
private String sJava;//成绩
public String getSSO() {
return sSO;
}
public void setSSO(String sso) {
sSO = sso;
}
public String getSName() {
return sName;
}
public void setSName(String name) {
sName = name;
}
public String getSSex() {
return sSex;
}
public void setSSex(String sex) {
sSex = sex;
}
public String getSAge() {
return sAge;
}
public void setSAge(String age) {
sAge = age;
}
public String getSJava() {
return sJava;
}
public void setSJava(String java) {
sJava = java;
}
}

2.测试类
package com.test;

public class textStudent {

/**
* @param args
*/
public static void main(String[] args) {
Student s1 = new Student();//学生对象一
Student s2 = new Student();//学生对象二
//学生一信息
s1.setSName("Tom");
s1.setSSO("1001002");
s1.setSAge("18");
s1.setSSex("男");
s1.setSJava("89");
//学生二信息
s2.setSName("Lucy");
s2.setSSO("1001001");
s2.setSAge("19");
s2.setSSex("女");
s2.setSJava("90");

System.out.println("两名学生的信息如下:");
System.out.println("学号\t姓名\t年龄\t性别\t成绩");
System.out.println(s1.getSSO()+"\t"+s1.getSName()+"\t"+s1.getSAge()+"\t"+s1.getSSex()+"\t"+s1.getSJava());
System.out.println(s2.getSSO()+"\t"+s2.getSName()+"\t"+s2.getSAge()+"\t"+s2.getSSex()+"\t"+s2.getSJava());
System.out.println();
int sj1 = Integer.parseInt(s1.getSJava());
int sj2 = Integer.parseInt(s2.getSJava());
double sum = (sj1+sj2)/2.0;//保留一位小数
System.out.println("平均成绩为:"+sum);
}

}

网友(2):

楼主问这种基础东西,是初学者,楼上那样写交给老师的话,老师一看就知道不是自己写的了。
(1)
public class Student {
String sNO;//学号
String sName;//姓名
char sSex;//性别
int sAge;//年龄
int sJava;//成绩
public String getNO() {
return sNO;
}
public String getName() {
return sName;
}
public char getSex() {
return sSex;
}
public int getSAge() {
return sAge;
}
public int getJava() {
return sJava;
}
}
(2,3)
public class Test {
public static void main(String[] args) {
Student s1 = new Student();//学生对象一
Student s2 = new Student();//学生对象二
//学生一信息
s1.sNO="430331";
s1.sName="Lucy";
s1.sSex='女';
s1.sAge=19;
s1.sJava=88;
//学生二信息
s2.sNO="430332";
s2.sName="Lily";
s2.sSex='女';
s2.sAge=19;
s2.sJava=87;

System.out.println("两名学生的信息如下:");
System.out.println("学号\t姓名\t年龄\t性别\t成绩");
System.out.println(s1.sNO+"\t"+s1.sName+"\t"+s1.sAge+"\t"+s1.sSex+"\t"+s1.sJava);
System.out.println(s2.sNO+"\t"+s2.sName+"\t"+s2.sAge+"\t"+s2.sSex+"\t"+s2.sJava);
System.out.println("两学生的平均成绩是"+(s1.sJava+s2.sJava)/2.0);
}

}