java编程代码

源代码在线查看: golfscores.java

软件大小: 355 K
上传用户: fq335288450
关键词: java 编程代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				import java.util.ArrayList;
				import java.util.Scanner;
				
				public class GolfScores
				{
				    /**
				     Shows differences between each of a list of golf scores and their average.
				    */
				    public static void main(String[] args) 
				    {
				       ArrayList score = new ArrayList ( );
				
				        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:");
				        fillArrayList(score);
				        showDifference(score);
				    }
				
				    /**
				     Reads values into the array a.
				    */
				    public static void fillArrayList(ArrayList a) 
				    {
				        System.out.println("Enter a list of 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)
				        {
				            a.add(next);
				            next = keyboard.nextDouble( );
				        }
				    } 
				    
				    /**
				      Returns the average of numbers in a.
				    */
				    public static double computeAverage(ArrayList a)
				    {
				        double total = 0;
				        for (Double element : a)
				            total = total + element;
				
				        int numberOfScores = a.size( );
				        if (numberOfScores > 0)
				        {
				            return (total/numberOfScores);
				        }
				        else
				        {
				            System.out.println("ERROR: Trying to average 0 numbers.");
				            System.out.println("computeAverage returns 0.");
				            return 0;
				        }
				    }
				
				    /**
				      Gives screen output showing how much each of the elements
				      in a differ from their average.
				    */
				    public static void showDifference(ArrayList a)
				    {
				        double average = computeAverage(a);
				        System.out.println("Average of the " + a.size( )
				                                             + " scores = " + average);
				        System.out.println("The scores are:");
				        for (Double element : a)
				            System.out.println(element + " differs from average by "
				                                             + (element - average));
				    }
				}
							

相关资源