2009年6月1日 星期一

Lab Modular Sorting

Write a sort method which takes a double array as parameter
and returns the sorted array. Call this method in a main program.
Hint: The lab is a rewriting of Lab Sorting
to make the sorting procedure a reusable method.


------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class LabModularSorting {
public static void sort (double score[])
{
int i, j;
double temp;
for(i=0; i<score.length; i++ )
for(j=0; j<score.length-1; j++)
if(score[j] < score[j+1])
{
temp = score[j];
score[j] = score[j+1];
score[j+1] = temp;
}//if
}//sort
    public static void main (String args[]) {
        Scanner keyboard = new Scanner(System.in);
int i;
double[] score = new double[5];
        System.out.println("Enter 5 scores:");
for(i = 0; i < score.length; i++)
score[i] = keyboard.nextDouble();
sort(score);
for(i = 0; i < score.length; i++)
System.out.println(score[i]);
    }//main
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

沒有留言:

張貼留言