【问题标题】:Help with a program that needs to read a data file and output scores帮助需要读取数据文件和输出分数的程序
【发布时间】:2011-05-17 23:03:40
【问题描述】:

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

在跳水比赛中,每位参赛者的分数是通过删除最低和最高分数来计算的 然后将剩余的分数相加。编写一个程序,读取提供的数据文件,格式为 如下表所示。对于每个潜水员,使用上述输出潜水员的姓名和总分 计分规则。将每位潜水员的总分格式化为小数点后两位。例如,输出 下面的陈若琳是:陈若琳——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());
    }
}
}

我绝不是程序员,看到代码我就头疼。我的问题是:如何让它正确阅读分数。名称输出正常,但分数基本上是一串乱码。我错过了什么导致这个问题?

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

【问题讨论】:

    标签: java datareader


    【解决方案1】:

    您正在阅读分数,而不是打印它们。

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

    你的程序也是,只是读取文件的第一条记录,我会留给你修改这段代码来读取所有记录。

    【讨论】:

    • 我不确定我是否遵循。它现在可以正确打印出所有分数,但它没有读取所有分数?我认为因为它只读取第一条记录,我将无法添加每个潜水员的分数?
    • 您必须循环读取每个驱动程序的所有分数。您还必须将各个分数相加才能得到总分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多