帮助需要读取数据文件并输出分数的程序

发布于 2024-11-08 02:11:44 字数 2834 浏览 4 评论 0原文

所以我正在研究这个程序(显然是家庭作业):

在跳水比赛中,每个参赛者的分数是通过去掉最低分和最高分来计算的 然后将剩余的分数相加。编写一个程序,读取提供的数据文件,格式为 如下表所示。对于每个潜水员,使用上述输出潜水员的姓名和总分 评分规则。将每位潜水员的总分格式化为小数点后两位。例如,输出为 下面的陈若琳为:陈若琳 – 56.90 分。

数据文件:

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

潜水员级别:

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();
}
}

潜水员测试计划:

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());
    }
}
}

我是不是程序员,我看着代码就头疼。我的问题是:如何让它正确读取分数。名字输出得很好,但分数基本都是一串乱码。我错过了什么导致这个问题?

任何帮助都会很棒。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-11-15 02:11:44

你正在阅读乐谱,只是没有打印它们。

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());
        }
    }
}

您的程序也只是读取文件的第一条记录,我将由您修改此代码以读取所有记录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文