Java Reference
In-Depth Information
System.out.println(
"This program reads golf scores and shows");
System.out.println(
"how much each differs from the average.");
System.out.println("Enter golf scores:");
fillArray( );
showDifference( );
}
/**
Reads values into the array score.
*/
public static void fillArray( )
{
System.out.println("Enter up to " + score.length
+ " nonnegative numbers:");
System.out.println(
"Mark the end of the list with a negative number.");
Scanner keyboard = new Scanner(System.in);
double next;
int index = 0;
next = keyboard.nextDouble( );
while ((next >= 0) && (index < score.length))
{
score[index] = next;
index++;
next = keyboard.nextDouble( );
//index is the number of
//array indexed variables used so far .
}
//index is the total number of array indexed variables used.
if (next >= 0)
System.out.println("Could only read in "
+ score.length + " input values.");
numberUsed = index;
}
/**
Precondition: numberUsed <= score.length.
score[0] through score[numberUsed-1] have values.
Returns the average of numbers ascore[0] through
score[numberUsed-1].
*/
Search WWH ::




Custom Search