我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*filename:P6Q18
*Name:Wang Chaoran
Description:
(Sorting students) Write a program that prompts the user to enter the number of students,
and student names and their scores, and prints the student names in decreasing order of their scores.
*/
import java.util.Scanner;
class P6Q18{
public static void main(String[] args){
System.out.println("Please enter the number of students");
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
double[] input=new double[number];
String[] inputName=new String[number];
for(int i=0;i<input.length;i++){
System.out.println("Please enter the name of student "+(i+1));
inputName[i]= scan.next();
System.out.println("Please enter the score of student "+(i+1));
input[i]=scan.nextDouble();
}
System.out.println("Original order of Name:");
print(inputName);
System.out.println("Original order of score:");
print(input);
insertionSort(input,inputName);
System.out.println("Name in decreasing order of their scores");
print(inputName);
System.out.println("Decreasing order of their scores");
print(input);
}
static void insertionSort(double[] array,String[] name){
double current;
String currentName="";
for(int i=1;i<array.length;i++){
current = array[i];
currentName = name[i];
int j;
for(j=i-1;j>=0&¤t>array[j];j--){
array[j+1]=array[j];
name[j+1]=name[j];
}
array[j+1]=current;
name[j+1]=currentName;
}
}
static void print(double[] array){
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
System.out.println();
}
static void print(String[] array){
for(int i=0;i<array.length;i++)
System.out.print(array[i]+" ");
System.out.println();
}
}
导入论坛查看(67)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing