Help with a program that needs to read a data file and output scores

血红的双手。 提交于 2019-12-13 06:05:39

问题


So I am working on this program (obviously homework):

In a diving competition, each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program that reads the provided data file formatted as depicted in the following table. For each diver output the diver's name and total score using the above scoring rules. Format each diver's total score to two decimal places. So for example, the output for Chen Ruolin below would be: Chen Ruolin – 56.90 points.

Data File:

Chen Ruolin          9.2   9.3   9     9.9   9.5   9.5   9.6   9.8     
Emilie Heymans       9.2   9.2   9     9.9   9.5   9.5   9.7   9.6     
Wang Xin             9.2   9.2   9.1   9.9   9.5   9.6   9.4   9.8     
Paola Espinosa       9.2   9.3   9.2   9     9.5   9.3   9.6   9.8     
Tatiana Ortiz        9.2   9.3   9     9.4   9.1   9.5   9.6   9.8     
Melissa Wu           9.2   9.3   9.3   9.7   9.2   9.2   9.6   9.8     
Marie-Eve Marleau    9.2   9.2   9.2   9.9   9.5   9.2   9.3   9.8     
Tonia Couch          9.2   9     9.1   9.5   9.2   9.3   9.4   9.6     
Laura Wilkinson      9.7   9.1   9.3   9.4   9.5   9.4   9.6   9.2

Diver Class:

import java.util.Vector;

public class Diver
{
private String firstName;
private String lastName;
private Vector scores;

public Diver()
{
    firstName = "";
    lastName = "";
    scores = new Vector();
}

public Diver(String firstName, String lastName, double... scores)
{
    this.firstName = firstName;
    this.lastName = lastName;
    this.scores = new Vector();

    for (double score : scores)
    {
        this.scores.add( score );
    }
}

public String getFirstName()
{
    return firstName;
}

public void setFirstName(String firstName)
{
    this.firstName = firstName;
}

public String getLastName()
{
    return lastName;
}

public void setLastName(String lastName)
{
    this.lastName = lastName;
}



public String toString()
{
    return firstName + " " + lastName + scores.toString();
}
}

Diver Test Program:

import java.io.*;
import java.util.*;

class TestDiver
{
public static void main(String[] args)
{
    try {
        Scanner scanner = new Scanner(new File("diving_data.txt"));
        scanner.useLocale(Locale.US);

        double[] scores = new double[8];

        while (scanner.hasNext())
        {
            String firstName = scanner.next();
            String lastName  = scanner.next();

            System.out.println("Diver: " + firstName + " " + lastName + " " + scores);

            double min = Double.MIN_VALUE;
            double max = Double.MAX_VALUE;


            for (int i = 0; i < scores.length; i++)
            {
                scores[i] = scanner.nextDouble();
            }

            Diver diver = new Diver(firstName, lastName, scores);
        }
        scanner.close();
    }
    catch (Exception e)
    {
        System.out.println("Problem: " + e.getMessage());
    }
}
}

I am by no means a programmer and my head hurts from looking at the code. My question is this: How do I get it to read the scores properly. The names are output fine, but the scores are basically a string of garbled characters. What am I missing that is causing this problem?

Any help would be great. Thanks.


回答1:


You were reading scores, you were just not printing them.

import java.io.*;
import java.util.*;

class TestDiver
{
    public static void main(String[] args)
    {
        try {
            Scanner scanner = new Scanner(new File("diving_data.txt"));
            scanner.useLocale(Locale.US);

            double[] scores = new double[8];

            while (scanner.hasNext())
            {
                String firstName = scanner.next();
                String lastName  = scanner.next();

                // scores is not defined here yet
                System.out.println("Diver: " + firstName + " " + lastName);

                double min = Double.MIN_VALUE;
                double max = Double.MAX_VALUE;


                for (int i = 0; i < scores.length; i++)
                {
                    scores[i] = scanner.nextDouble();

                    // prints scores
                    System.out.println(" " + scores[i]);
                }

                Diver diver = new Diver(firstName, lastName, scores);
            }
            scanner.close();
        }
        catch (Exception e)
        {
            System.out.println("Problem: " + e.getMessage());
        }
    }
}

Your program also, just reads the first record of the file, I will leave up to you to modify this code to read all the records.



来源:https://stackoverflow.com/questions/6038143/help-with-a-program-that-needs-to-read-a-data-file-and-output-scores

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!