java创建类和类的引用文档
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
package data;//创建学生类
public class Student {
private String studentName;
private int studentScore;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) { //Scanner input=new Scanner(System.in)
this.studentName = studentName;
}
public int getStudentScore() {
return studentScore;
}
public void setStudentScore(int studentScore) {
this.studentScore = studentScore;
}
public String toString(){
return "姓名"+studentName+"分数"+studentScore;
}
}
package data;
import java.util.Scanner;
public class StudentTest {
/**
* @param args
输入学生姓名,排序,输出
*/
public static void main(String[] args) {
int i=0;
Student[] stu=new Student[5];
Scanner input =new Scanner(System.in);
for(i=0;i<5;i++){
//stu=new Student();
System.out.println("please input name and score");
stu[i] = new Student();
stu[i].setStudentName(input.next());
stu[i].setStudentScore(input.nextInt());
}
for(i=1;i<5;i++){
for(int j=0;j<5-i;j++){
if(stu[j].getStudentScore()>stu[j+1].getStudentScore()){
Student temp = new Student();
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp ;
}
}
}
for(i=0;i<5;i++){
System.out.println(stu[i]);
}
}
}