import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
System.out.print("Enter ten names and ten scores: ");
Scanner input = new Scanner(System.in);
String[] name = new String[10];
double[] score = new double[10];
for(int i = 0; i < name.length; i++)
{
name[i] = input.next();
score[i] = input.nextDouble();
}
sort(name, score);
System.out.println("After sorted: ");
for(int i = 0; i < name.length; i++)
System.out.println(name[i] + " ");
}
public static void sort(String[] name, double[] score)
{
for(int i = 0; i < score.length - 1; i++)
{
double currentMax = score[i];
int currentIndex = i;
for(int j = 1; j < score.length; j++)
{
if(score[j] > currentMax)
{
currentMax = score[j];
currentIndex = j;
}
}
if(currentIndex != i)
{
score[currentIndex] = score[i];
score[i] = currentMax;
String tmp = name[i];
name[i] = name[currentIndex];
name[currentIndex] = tmp;
}
}
}
}
6-19 编程练习题答案
最新推荐文章于 2021-11-02 01:15:00 发布