编写一个Java程序,提示用户输入学生数量,学生姓名和他们的成绩,并按照成绩的降序打印学生姓名

2024-12-03 09:10:41
推荐回答(3个)
回答1:

import java.util.Scanner;

public class StudentInAndOut {
private String name;
private long grade;

public long getGrade() {
return grade;
}
public void setGrade(long grade) {
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String toString(){
return this.name;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生数");
int stuNumT = sc.nextInt();
StudentInAndOut objArr[] = new StudentInAndOut[stuNumT];
for(int i = 0; i < objArr.length; i++){
objArr[i] = new StudentInAndOut();
System.out.println("请输入第" + (i+1) + "个学生的姓名");
String name = sc.next();
objArr[i].setName(name);
System.out.println("请输入第" + (i+1) + "个学生的成绩");
long grade = sc.nextLong();
objArr[i].setGrade(grade);
}
System.out.println("打印开始:");
for(int i = 0; i < objArr.length; i++){
for(int j = i+1; j < objArr.length; j++){
if(objArr[i].getGrade() < objArr[j].getGrade()){
StudentInAndOut s = new StudentInAndOut();
s = objArr[i];
objArr[i] = objArr[j];
objArr[j] = s;
}
}
}
for(int i=0; i < objArr.length; i++){
System.out.println(objArr[i].getName()+"的成绩是:"+objArr[i].getGrade());
}
}
}

回答2:

import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
import java.util.Iterator;
public class PrintStuTest {
public static void main(String[] args) {
System.out.println("请输入学生数:");
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
Map stu = new TreeMap();
int time = 0;
while(time System.out.println("请输入学生" + (time+1) + "的姓名和成绩:");
String name = scan.next();
float sco = scan.nextFloat();
stu.put(name, sco);
time++;
}
Iterator iter = stu.keySet().iterator();
String[] str = new String[count];
time = 0;
while(iter.hasNext()) {
str[time] = iter.next();
time++;
}
System.out.println("成绩降序,学生姓名:");
for(int i=0; i System.out.print(str[count-1-i] + " ");
}
}
}
//输入姓名和成绩需用空格隔开,成绩为 float 型

回答3:

你的问题描述的不是很清楚。有没有数据库,而且输入的学生数量、学生姓名、成绩到底有什么用?