为什么下面的代码不产生任何输出?

问题描述 投票:0回答:1

已经要求您使用Java代码编写一份全国橄榄球联盟(NFL)通过和接收统计信息的报告。四分卫和广泛的接收者有一些共同的信息,但是其他信息则针对他们的位置。

public class Player {

String firstName, lastName, team, position;

/**
* @param firstName
* @param lastName
* @param team
* @param position
*/
public Player(String firstName, String lastName, String team,
String position) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.team = team;
this.position = position;
}

/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}

/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}

/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* @return the team
*/
public String getTeam() {
return team;
}

/**
* @param team
* the team to set
*/
public void setTeam(String team) {
this.team = team;
}

/**
* @return the position
*/
public String getPosition() {
return position;
}

/**
* @param position
* the position to set
*/
public void setPosition(String position) {
this.position = position;
}

}

public class Quarterback extends Player {

int completions, attempts, yards;

/**
* @param firstName
* @param lastName
* @param team
* @param position
* @param completions
* @param attempts
* @param yards
*/
public Quarterback(String firstName, String lastName, String team,
String position, int completions, int attempts, int yards) {
super(firstName, lastName, team, position);
this.completions = completions;
this.attempts = attempts;
this.yards = yards;
}

/**
* @return the completions
*/
public int getCompletions() {
return completions;
}

/**
* @param completions
* the completions to set
*/
public void setCompletions(int completions) {
this.completions = completions;
}

/**
* @return the attempts
*/
public int getAttempts() {
return attempts;
}

/**
* @param attempts
* the attempts to set
*/
public void setAttempts(int attempts) {
this.attempts = attempts;
}

/**
* @return the yards
*/
public int getYards() {
return yards;
}

/**
* @param yards
* the yards to set
*/
public void setYards(int yards) {
this.yards = yards;
}

/**
* percent returns ratio of completions to attempts
*
* @return
*/
public double percent() {
return ((double) completions / (double) attempts) * 100.0d;

}

/**
* yardsPerAttempt returns number of yards / attempts
*
* @return
*/
public double yardsPerAttempt() {

return (double) yards / (double) attempts;

}

/**
* yardsPerGame returns average yards per game (assume 16 games)
*
* @return
*/
public double yardsPerGame() {

return (double) yards / (double) 16.0d;
}

}

public class Receiver extends Player {

int receptions;
int yards;

/**
* @param firstName
* @param lastName
* @param team
* @param position
* @param receptions
* @param yards
*/
public Receiver(String firstName, String lastName, String team,
String position, int receptions, int yards) {
super(firstName, lastName, team, position);
this.receptions = receptions;
this.yards = yards;
}

/**
* @return the receptions
*/
public int getReceptions() {
return receptions;
}

/**
* @param receptions
* the receptions to set
*/
public void setReceptions(int receptions) {
this.receptions = receptions;
}

/**
* @return the yards
*/
public int getYards() {
return yards;
}

/**
* @param yards
* the yards to set
*/
public void setYards(int yards) {
this.yards = yards;
}

/**
* yardsPerReception returns number of yards / receptions
*
* @return
*/
public double yardsPerReception() {

return (double) yards / (double) receptions;

}

/**
* yardsPerGame returns average yards per game (assume 16 games)
*
* @return
*/
public double yardsPerGame() {

return (double) yards / 16.0d;

}
}

import java.io.File;
import java.text.DecimalFormat;
import java.util.Scanner;

public class NFLTestPlayer {

/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = null;
try {
Player players[] = new Player[6];
scanner = new Scanner(new File("nfl.txt"));
int count = 0;
while (scanner.hasNext()) {
String firstName, lastName, team, position;
firstName = scanner.next();
lastName = scanner.next();
team = scanner.next();
position = scanner.next();
if (position.equals("QB")) {
int completions, attempts, yards;
completions = scanner.nextInt();
attempts = scanner.nextInt();
yards = scanner.nextInt();
players[count++] = new Quarterback(firstName, lastName,
team, position, completions, attempts, yards);

} else if (position.equals("WR")) {

int receptions;
int yards;
receptions = scanner.nextInt();
yards = scanner.nextInt();
players[count++] = new Receiver(firstName, lastName, team,
position, receptions, yards);

}

}

System.out.println("NFL 2015 Player Passing/Receiving Statistics");
System.out.println("Quarterbacks");
DecimalFormat decimalFormat = new DecimalFormat("#.##");
System.out
.println("Player\t\tTeam\tComp\tAtt\tPct\tYds\tYds/A\tYds/G");
for (int i = 0; i < players.length; i++) {
if (players[i] instanceof Quarterback) {
Quarterback quarterback = (Quarterback) players[i];
System.out.println(quarterback.getFirstName()
+ ","
+ quarterback.getLastName()
+ "\t"
+ quarterback.getTeam()
+ "\t"
+ quarterback.getCompletions()
+ "\t"
+ quarterback.getAttempts()
+ "\t"
+ decimalFormat.format(quarterback.percent())
+ "\t"
+ quarterback.getYards()
+ "\t"
+ decimalFormat.format(quarterback
.yardsPerAttempt()) + "\t"
+ decimalFormat.format(quarterback.yardsPerGame()));

}

}

System.out.println("Wide Receivers");

System.out
.println("Player\t\tTeam\tComp\tAtt\tPct\tYds\tYds/A\tYds/G");
for (int i = 0; i < players.length; i++) {
if (players[i] instanceof Receiver) {
Receiver receiver = (Receiver) players[i];
System.out
.println(receiver.getFirstName()
+ ","
+ receiver.getLastName()
+ "\t"
+ receiver.getTeam()
+ "\t"
+ receiver.getReceptions()
+ "\t"
+ receiver.getYards()
+ "\t"
+ decimalFormat.format(receiver
.yardsPerReception())
+ "\t"
+ decimalFormat.format(receiver
.yardsPerGame()));

}
}

} catch (Exception e) {
// TODO: handle exception
}
}
}

输出应使用以下文本文件创建表:

nfl.txt

Ben Roethlisberger PIT QB 452 675 5129
Julio Jones ATL WR 113 1677 
Aaron Rodgers GB QB 372 597 4442
Patrick Mahomes KC QB 383 580 5097
JuJuj Smith-Schuster PIT WR 111 1426 
Philip Rivers LAC QB 347 508 4308
DeAndre Hopkins HOU WR 115 1572 
Mike Evans TB WR 86 1524 
Matt Ryan ATL QB 422 608 4924
Davante Adams GB WR 111 1386 
Tyreek Hill KC WR 87 1479 
Michael Thomas NO WR 125 1405 
Jared Goff LAR QB 364 561 4688
Andrew Luck IND QB 430 639 4593
Adam Thielen MIN WR 113 1373 
Eli Manning NYG QB 380 576 4299
Kirk Cousins MIN QB 425 606 4298
Robert Woods LAR WR 86 1219 
Tom Brady NE QB 375 570 4355
Brandin Cooks LAR WR 80 1204
java eclipse polymorphism
1个回答
0
投票

例外现在正在告诉您确切的错误。您仅将数组定义为包含6个元素Player players[] = new Player[6];,并且文本文件包含6个以上的元素。

© www.soinside.com 2019 - 2024. All rights reserved.