编写一个Java程序,提示用户输入学生数量,学生姓名和他们的成绩,并按照成绩的降序打印学生姓名
3个回答
展开全部
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());
}
}
}
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());
}
}
}
展开全部
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<String, Float> stu = new TreeMap<String, Float>();
int time = 0;
while(time<count) {
System.out.println("请输入学生" + (time+1) + "的姓名和成绩:");
String name = scan.next();
float sco = scan.nextFloat();
stu.put(name, sco);
time++;
}
Iterator<String> 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<count; i++) {
System.out.print(str[count-1-i] + " ");
}
}
}
//输入姓名和成绩需用空格隔开,成绩为 float 型
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<String, Float> stu = new TreeMap<String, Float>();
int time = 0;
while(time<count) {
System.out.println("请输入学生" + (time+1) + "的姓名和成绩:");
String name = scan.next();
float sco = scan.nextFloat();
stu.put(name, sco);
time++;
}
Iterator<String> 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<count; i++) {
System.out.print(str[count-1-i] + " ");
}
}
}
//输入姓名和成绩需用空格隔开,成绩为 float 型
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的问题描述的不是很清楚。有没有数据库,而且输入的学生数量、学生姓名、成绩到底有什么用?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询