8 个解决方案
#1
冒泡排序走两趟即可……
#2
使用两个变量保存即可
#3
List<StudentInfo> list = new arrayList();
list.put(studentInfo);
for (int i = 0; i < list.size()-1; i++){
for(int j = 0 ;j <list.size() - i - 1; j++){
if(list[j].score < list[j + 1].score){
StudentInfo temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
} //取出最后两个就是成绩前两名了,不知道有没有错,坐等大神解释
list.put(studentInfo);
for (int i = 0; i < list.size()-1; i++){
for(int j = 0 ;j <list.size() - i - 1; j++){
if(list[j].score < list[j + 1].score){
StudentInfo temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
} //取出最后两个就是成绩前两名了,不知道有没有错,坐等大神解释
#4
不需要排序,就一趟循环就可以解决,试试我这个代码。
import java.util.ArrayList;
public class Find1_2 {
public static void main(String[] args) {
ArrayList<StudentInfo> list = new ArrayList<StudentInfo>();
//生成实验数据,并显示
for(int i =0; i<12; i++ ){
StudentInfo info = new StudentInfo("stu"+(i+1), Math.random()*100);
System.out.println(info);
list.add(info);
}
StudentInfo info1 = new StudentInfo("tmp", 0);
StudentInfo info2 = new StudentInfo("tmp", 0);
int count = list.size();
for(int i =0; i<count; i++ ){
StudentInfo info = list.get(i);
if(info.score>info1.score){
info2 = info1;
info1 = info;
}else if(info.score>info2.score){
info2 = info;
}
}
//显示运行结果
System.out.println("第一名是"+info1+", 第二名是"+info2);
}
}
class StudentInfo{
double score;
String name;
public StudentInfo(String name, double score){
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student[name="+name+",score="+score+"]";
}
}
#5
这个不是太难,你应该是新手吧?楼上说的都没错,但是对于初学者来说,有些难度,我想还是利用流程控制和循环结构来实现比较和你的心意吧?
#6
#7
什么都不需要,直接定义两个变量记录最高分的最低分的,每次输入都作比较,就行了
#8
Scanner input = new Scanner(System.in);
int count = 0;
System.out.println("Enter number of students");
int number = input.nextInt();
System.out.println("Enter a student name");
String student1 = input.next();
System.out.println("Enter a student score");
double score1 = input.nextDouble();
System.out.println("Enter a student name");
String student2 = input.next();
System.out.println("Enter a student score");
double score2 = input.nextDouble();
/*
* 保证socre1在socre1和socre2中一定为最大的值
* */
if (score1<score2) {
double temp=0;
String name=null;
temp=score1;
score1=score2;
score2=temp;
name=student1;
student1=student2;
student2=name;
//System.out.println(score1);看交换是否完成,
}
for (int i = 0; i < number - 2; i++) {
System.out.println("Enter a student name");
String student = input.next();
System.out.println("Enter a student score");
double score = input.nextDouble();
/*
* 从第三个输入开始于前面两个数比较,
* 大于前面两个数中的最大值,score1,则将score1赋给score2,再将score赋给score1,
* 若score1>score>score2,则将score赋给score2
* */
if (score>score1) {
score2=score1;
student2=student1;
score1=score;
student1=student;
}
else if (score>score2) {
score2=score;
student2=student;
}
else {
}
}
System.out.println("The top student " + student1 + "'s score is "
+ score1);
System.out.println("The second student " + student2 + "'s score is "
+ score2);
int count = 0;
System.out.println("Enter number of students");
int number = input.nextInt();
System.out.println("Enter a student name");
String student1 = input.next();
System.out.println("Enter a student score");
double score1 = input.nextDouble();
System.out.println("Enter a student name");
String student2 = input.next();
System.out.println("Enter a student score");
double score2 = input.nextDouble();
/*
* 保证socre1在socre1和socre2中一定为最大的值
* */
if (score1<score2) {
double temp=0;
String name=null;
temp=score1;
score1=score2;
score2=temp;
name=student1;
student1=student2;
student2=name;
//System.out.println(score1);看交换是否完成,
}
for (int i = 0; i < number - 2; i++) {
System.out.println("Enter a student name");
String student = input.next();
System.out.println("Enter a student score");
double score = input.nextDouble();
/*
* 从第三个输入开始于前面两个数比较,
* 大于前面两个数中的最大值,score1,则将score1赋给score2,再将score赋给score1,
* 若score1>score>score2,则将score赋给score2
* */
if (score>score1) {
score2=score1;
student2=student1;
score1=score;
student1=student;
}
else if (score>score2) {
score2=score;
student2=student;
}
else {
}
}
System.out.println("The top student " + student1 + "'s score is "
+ score1);
System.out.println("The second student " + student2 + "'s score is "
+ score2);
#1
冒泡排序走两趟即可……
#2
使用两个变量保存即可
#3
List<StudentInfo> list = new arrayList();
list.put(studentInfo);
for (int i = 0; i < list.size()-1; i++){
for(int j = 0 ;j <list.size() - i - 1; j++){
if(list[j].score < list[j + 1].score){
StudentInfo temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
} //取出最后两个就是成绩前两名了,不知道有没有错,坐等大神解释
list.put(studentInfo);
for (int i = 0; i < list.size()-1; i++){
for(int j = 0 ;j <list.size() - i - 1; j++){
if(list[j].score < list[j + 1].score){
StudentInfo temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
} //取出最后两个就是成绩前两名了,不知道有没有错,坐等大神解释
#4
不需要排序,就一趟循环就可以解决,试试我这个代码。
import java.util.ArrayList;
public class Find1_2 {
public static void main(String[] args) {
ArrayList<StudentInfo> list = new ArrayList<StudentInfo>();
//生成实验数据,并显示
for(int i =0; i<12; i++ ){
StudentInfo info = new StudentInfo("stu"+(i+1), Math.random()*100);
System.out.println(info);
list.add(info);
}
StudentInfo info1 = new StudentInfo("tmp", 0);
StudentInfo info2 = new StudentInfo("tmp", 0);
int count = list.size();
for(int i =0; i<count; i++ ){
StudentInfo info = list.get(i);
if(info.score>info1.score){
info2 = info1;
info1 = info;
}else if(info.score>info2.score){
info2 = info;
}
}
//显示运行结果
System.out.println("第一名是"+info1+", 第二名是"+info2);
}
}
class StudentInfo{
double score;
String name;
public StudentInfo(String name, double score){
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student[name="+name+",score="+score+"]";
}
}
#5
这个不是太难,你应该是新手吧?楼上说的都没错,但是对于初学者来说,有些难度,我想还是利用流程控制和循环结构来实现比较和你的心意吧?
#6
#7
什么都不需要,直接定义两个变量记录最高分的最低分的,每次输入都作比较,就行了
#8
Scanner input = new Scanner(System.in);
int count = 0;
System.out.println("Enter number of students");
int number = input.nextInt();
System.out.println("Enter a student name");
String student1 = input.next();
System.out.println("Enter a student score");
double score1 = input.nextDouble();
System.out.println("Enter a student name");
String student2 = input.next();
System.out.println("Enter a student score");
double score2 = input.nextDouble();
/*
* 保证socre1在socre1和socre2中一定为最大的值
* */
if (score1<score2) {
double temp=0;
String name=null;
temp=score1;
score1=score2;
score2=temp;
name=student1;
student1=student2;
student2=name;
//System.out.println(score1);看交换是否完成,
}
for (int i = 0; i < number - 2; i++) {
System.out.println("Enter a student name");
String student = input.next();
System.out.println("Enter a student score");
double score = input.nextDouble();
/*
* 从第三个输入开始于前面两个数比较,
* 大于前面两个数中的最大值,score1,则将score1赋给score2,再将score赋给score1,
* 若score1>score>score2,则将score赋给score2
* */
if (score>score1) {
score2=score1;
student2=student1;
score1=score;
student1=student;
}
else if (score>score2) {
score2=score;
student2=student;
}
else {
}
}
System.out.println("The top student " + student1 + "'s score is "
+ score1);
System.out.println("The second student " + student2 + "'s score is "
+ score2);
int count = 0;
System.out.println("Enter number of students");
int number = input.nextInt();
System.out.println("Enter a student name");
String student1 = input.next();
System.out.println("Enter a student score");
double score1 = input.nextDouble();
System.out.println("Enter a student name");
String student2 = input.next();
System.out.println("Enter a student score");
double score2 = input.nextDouble();
/*
* 保证socre1在socre1和socre2中一定为最大的值
* */
if (score1<score2) {
double temp=0;
String name=null;
temp=score1;
score1=score2;
score2=temp;
name=student1;
student1=student2;
student2=name;
//System.out.println(score1);看交换是否完成,
}
for (int i = 0; i < number - 2; i++) {
System.out.println("Enter a student name");
String student = input.next();
System.out.println("Enter a student score");
double score = input.nextDouble();
/*
* 从第三个输入开始于前面两个数比较,
* 大于前面两个数中的最大值,score1,则将score1赋给score2,再将score赋给score1,
* 若score1>score>score2,则将score赋给score2
* */
if (score>score1) {
score2=score1;
student2=student1;
score1=score;
student1=student;
}
else if (score>score2) {
score2=score;
student2=student;
}
else {
}
}
System.out.println("The top student " + student1 + "'s score is "
+ score1);
System.out.println("The second student " + student2 + "'s score is "
+ score2);