统计信息

  • 访问数:14546
  • 博客数:143
  • 建立时间:2008-01-05
  • 更新时间:2008-10-15
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q18

2008-04-17 14:09:16

天气: 晴朗 心情: 高兴

/*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&&current>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();
  }
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd