编写程序,提示用户输入学生的个数,每个学生的名字及其分数,最后显示最高分的学生和第二高分的学生
答案:2 悬赏:50
解决时间 2021-03-11 18:16
- 提问者网友:留有余香
- 2021-03-11 08:23
编写程序,提示用户输入学生的个数,每个学生的名字及其分数,最后显示最高分的学生和第二高分的学生
最佳答案
- 二级知识专家网友:你可爱的野爹
- 2021-03-11 08:48
Student.java
import java.io.*;
import java.util.*;
public class Student{
public static void main( String[] args){
int n;
Scanner in = new Scanner(System.in);
System.out.println("请输入学生的个数:");
n = in.nextInt();
String[] stu=new String[n];
int[] score = new int[n];
for(int i=0;i stu[i] = in.next();
score[i]= in.nextInt();
}
int first,second,first_i,second_i;
first = score[0];
second= score[0];
first_i= 0;
second_i=0;
for(int i=1;i if(score[i] >second){
second = score[i];
second_i = i;
if(second > first){
int t = second;
second = first;
first = t;
t = second_i;
second_i = first_i;
first_i = t;
}
}
}
System.out.println("最高分学生:" + stu[first_i]+" 分数:"+first);
System.out.println("第二高分学生:" + stu[second_i]+" 分数:"+second);
}
}
运行结果:
java Student
请输入学生的个数:
5
张三 1
李四 2
王五 3
赵八 3
孙九 5
最高分学生:孙九 分数:5
第二高分学生:王五 分数:3
import java.io.*;
import java.util.*;
public class Student{
public static void main( String[] args){
int n;
Scanner in = new Scanner(System.in);
System.out.println("请输入学生的个数:");
n = in.nextInt();
String[] stu=new String[n];
int[] score = new int[n];
for(int i=0;i
score[i]= in.nextInt();
}
int first,second,first_i,second_i;
first = score[0];
second= score[0];
first_i= 0;
second_i=0;
for(int i=1;i
second = score[i];
second_i = i;
if(second > first){
int t = second;
second = first;
first = t;
t = second_i;
second_i = first_i;
first_i = t;
}
}
}
System.out.println("最高分学生:" + stu[first_i]+" 分数:"+first);
System.out.println("第二高分学生:" + stu[second_i]+" 分数:"+second);
}
}
运行结果:
java Student
请输入学生的个数:
5
张三 1
李四 2
王五 3
赵八 3
孙九 5
最高分学生:孙九 分数:5
第二高分学生:王五 分数:3
全部回答
- 1楼网友:三千妖杀
- 2021-03-11 09:32
public static void main(String[] args) throws Exception {
Scanner in=new Scanner(System.in);
System.out.println("请输入学生的个数:");
int num=in.nextInt();
String names[]=new String [num];//学生姓名数组
double scores[]=new double [num];//学生分数数组
double max=0; //记录最大分数
int index=0; //记录最大分数的下标
for (int i = 0; i <num; i++) {
System.out.print("请输入第"+(i+1)+"个学生的姓名:");
names[i]=in.next();
System.out.print("请输入第"+(i+1)+"个学生的分数:");
scores[i]=in.nextDouble();
if(scores[i]>max){
max=scores[i];
index=i;
}
}
System.out.println("最高分的学生是:"+names[index]+",分数为:"+scores[index]);
}
Scanner in=new Scanner(System.in);
System.out.println("请输入学生的个数:");
int num=in.nextInt();
String names[]=new String [num];//学生姓名数组
double scores[]=new double [num];//学生分数数组
double max=0; //记录最大分数
int index=0; //记录最大分数的下标
for (int i = 0; i <num; i++) {
System.out.print("请输入第"+(i+1)+"个学生的姓名:");
names[i]=in.next();
System.out.print("请输入第"+(i+1)+"个学生的分数:");
scores[i]=in.nextDouble();
if(scores[i]>max){
max=scores[i];
index=i;
}
}
System.out.println("最高分的学生是:"+names[index]+",分数为:"+scores[index]);
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯