用java代码设计一个学生类

2025-04-30 21:52:40
推荐回答(4个)
回答1:

public class Student{
private String name;
private String studentNo;
private float score;
//get方法
//set方法

public static void main(String[] args){
//你可以用学生数组或者List来存数据
//我示范一下用数组存数据的 当然你用list来存的话 后面的比较 更加简单 只需要

//实现一个比较器即可
Student[] students = new Student[2]

Student a =new Student();
a.setName("a");
a.setStudentNo("001");
a.setScore(98);
students[0]=a;
Student b = new Student();
b.setName("b");
b.setStudentNo("002");
b.setScore(87);
sudents[1] = b;
。。。。。。

//然后用冒泡排序根据学生的成绩判断 算法你自己决定 冒泡还是挺好用的
for(int i =0,len =students.length ;i for(int j=i+1,len;=students.length;j
if(students[i].getScore()>students[j].getScore()){
Student temp =students[i];
students[i] = students[j];

students[j]=temp;
}

}
}
///最后输出结果
for(){
System.out.println();
}
}

回答2:

class Student
{
String name,num;
int score;
Student(String name1,String num1,int score1)
{
name=name1;
num=num1;
score=score1;
}
void Show()
{
System.out.println("姓名:"+name+" 学号:"+num+" 成绩:"+score);
}
}
public class Ex {

public static void main(String args[])
{
Student[] st=new Student[5];
st[0]=new Student("张三","0001",75);
st[1]=new Student("李四","0031",70);
st[2]=new Student("王五","0023",85);
st[3]=new Student("赵六","0043",65);
st[4]=new Student("李红","0014",90);
int max;
for(int i=0;i {
for(int j=i+1;j {
if(st[j].score>st[i].score)
{
max=st[j].score;
st[j].score=st[i].score;
st[i].score=max;
}
}
}
for(int i=0;i st[i].Show();
}
}

回答3:

public class User {
private String userName;
private String userNum;
private Integer score;
public User(){

}
public User(String userName, String userNum, Integer score) {
this.userName = userName;
this.userNum = userNum;
this.score = score;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserNum() {
return userNum;
}
public void setUserNum(String userNum) {
this.userNum = userNum;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}

}
import java.util.ArrayList;
import java.util.Iterator;
public class DealData {
/**
* 交换对象引用
* @param a
* @param b
*/
public static void compare(User a,User b){
User temp = new User();
if(a.getScore() {
temp = a ;
a = b ;
b = temp;
}
}
public static void sort(ArrayList list){
User temp = new User();
for(int i=0;i for(int j=0;j if(list.get(j).getScore() temp = list.get(j);
list.set(j, list.get(j+1));
list.set(j+1, temp);
}
}
}
for(User u:list){
System.out.println(u.getUserName()+":"+u.getScore());
}
}
public static void main(String[] args) {
ArrayList user = new ArrayList();

user.add(new User("张三","123",50));
user.add(new User("王","123",70));
user.add(new User("李飞","123",80));
user.add(new User("陈二","123",20));
user.add(new User("狗屁","123",44));
user.add(new User("网吧","123",33));
user.add(new User("王八","123",89));
user.add(new User("狗容","123",56));
DealData.sort(user);
}
}

回答4:

自己写方法的话循环比下分数就可以了