2009年3月17日 星期二

Lab Finding the max of a list of numbers

Based on your study of Display 3.8, write a code to find the max and min of a list of number.
For example, given 1,3,5, and9, the max is 9 and the min is 1.
Your program should be able to process a list of any length.


------------------------------------------------<程式碼開始>--------------------------------------

import java.util.Scanner;
public class s20090316
{
public static void main(String[] args)
{
Scanner Keyboard = new Scanner(System.in);
int i, q, p, temp=0;
System.out.println("write a code to find the max and min of a list of number.");
System.out.println("list?");
q = Keyboard.nextInt();
int[] Number = new int[q];
for(i=0;i<q;i++)
{
p=i+1;
System.out.println("The " + p + " number:");
Number[i] = Keyboard.nextInt();   
}
/*for(i=0, p=0;i<q;i++)
{
p=i+1;
System.out.println(p + " number:"+Number[i]);
}*/
for(int j=0;j<q;j++)
{
for(int k=0;k<q-1;k++)
{
if(Number[k]<Number[k+1])
{
temp = Number[k];
Number[k] = Number[k+1];
Number[k+1] = temp;
}
}   
}
System.out.println("Max = " + Number[0]); 
System.out.println("min = " + Number[q-1]); 
}
}
------------------------------------------------<程式碼結束>--------------------------------------

沒有留言:

張貼留言