-->

2007年10月22日 星期一

Homework 10-12-2007: Finding the max and the min

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 findmaxmin
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers you want to compare?");
int a = keyboard.nextInt();
double[] number = new double[a];
int b;
double min,max;
System.out.println("Enter the numbers you want to compare.");
number[0] = keyboard.nextDouble();
max = number[0];
min = number[0];
for (b = 1 ; b < a ; b++)
{
number[b] = keyboard.nextDouble();
if (number[b] > max)
max = number[b];
if (number[b] < min)
min = number[b];
}
System.out.println("The maxima number is "+max);
System.out.println("The minima number is "+min);
}

}

0 COMMENT: