2009年6月1日 星期一

Lab Array

Study Display 6.1, and then write a program that can sort numbers in ascending order.

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

沒有留言:

張貼留言