帮助需要读取数据文件并输出分数的程序
所以我正在研究这个程序(显然是家庭作业):
在跳水比赛中,每个参赛者的分数是通过去掉最低分和最高分来计算的 然后将剩余的分数相加。编写一个程序,读取提供的数据文件,格式为 如下表所示。对于每个潜水员,使用上述输出潜水员的姓名和总分 评分规则。将每位潜水员的总分格式化为小数点后两位。例如,输出为 下面的陈若琳为:陈若琳 – 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正在阅读乐谱,只是没有打印它们。
您的程序也只是读取文件的第一条记录,我将由您修改此代码以读取所有记录。
You were reading scores, you were just not printing them.
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.