为什么下面的代码没有产生正确的输出?

why is following code not producing the right output?

您被要求使用 Java 代码编写国家橄榄球联盟 (NFL) 传球和接球统计报告。四分卫和外接手有一些共同的信息,但其他信息是针对他们的位置的。

玩家Class

public class Player {

    String firstName; 
    String lastName; 
    String team;
    String position;

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

    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 getTeam() 
    {
        return team;
    }

    public void setTeam(String team) 
    {
        this.team = team;
    }

    public String getPosition() 
    {
        return position;
    }

    public void setPosition(String position) 
    {
        this.position = position;
    }
} 

四分卫Class

public class Quarterback extends Player {
    int completions; 
    int attempts;
    int 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;
    }

    public int getCompletions() 
    {
        return completions;
    }

    public void setCompletions(int completions) 
    {
        this.completions = completions;
    }

    public int getAttempts() 
    {
        return attempts;
    }

    public void setAttempts(int attempts)
    {
        this.attempts = attempts;
    }

    public int getYards() 
    {
        return yards;
    }

    public void setYards(int yards)
    {
        this.yards = yards;
    }

    public double percent() 
    {
        return ((double) completions / (double) attempts) * 100.0d;
    }

    public double yardsPerAttempt() 
    {
        return (double) yards / (double) attempts;
    }

    public double yardsPerGame() 
    {
        return (double) yards / (double) 16.0d;
    }
}

接收者Class

public class Receiver extends Player{
    int receptions;
    int 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;
    }

    public int getReceptions()
    {
        return receptions;
    }

    public void setReceptions(int receptions) 
    {
        this.receptions = receptions;
    }

    public int getYards()
    {
        return yards;
    }

    public void setYards(int yards)
    {
        this.yards = yards;
    }

    public double yardsPerReception() 
    {
        return (double) yards / (double) receptions;
    }

    public double yardsPerGame() 
    {
        return (double) yards / 16.0d;
    }
}

下面是我在显示输出时出现问题的地方。

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

public class NFLTestPlayer 
{

    public static void main(String[] args) 
    {
        Scanner scanner = null;

        try 
        {
            Player players[] = new Player[20];
            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 2018 Player Passing/Receiving Statistics\n");
            System.out.println("Quarterbacks");

            System.out.printf("%-25s %-5s %12s %12s %12s %12s %12s %12s\n",
                    "Player","Team", "Comp","Att","Pct","Yds","Yds/A","Yds/G");

            for (int i = 0; i < players.length; i++)
            {
                if (players[i] instanceof Quarterback) 
                {
                    Quarterback quarterback = (Quarterback) players[i];
                    System.out.printf("%-25s %-5s %12.0f %12.0f %12.0f %12.1f %12.2f %12d",
                            quarterback.getFirstName() 
                            + ","  + quarterback.getLastName() 
                            + quarterback.getTeam()
                            + quarterback.getCompletions()
                            + quarterback.getAttempts()

                            + quarterback.getYards()
                            +quarterback.yardsPerAttempt() 
                            +quarterback.yardsPerGame());
                    }
                }

            System.out.println("\n");
            System.out.println("Wide Receivers");
            System.out.printf("%-25s %-5s %12s %12s %12s %12s\n",
                    "Player","Team", "Rec", "Yds","Yds/R","Yds/G");


            for (int i = 0; i < players.length; i++) 
            {
                if (players[i] instanceof Receiver)
                {
                    Receiver receiver = (Receiver) players[i];
                    System.out.printf("%-25s %-5s %12.0f %12.0f %12.0f %12.1f",
                            receiver.getFirstName()
                            + ","  + receiver.getLastName()
                            + receiver.getTeam()
                            + receiver.getReceptions()
                            + receiver.getYards()
                            + receiver.yardsPerReception()
                            + receiver.yardsPerGame());
                    }
                }
            } catch (Exception e)
        {
                e.printStackTrace();        
        }
    }
}

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

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


NFL 2018 Player Passing/Receiving Statistics            
Quarterbacks                            
Player         Team Comp Att Pct    Yds    Yds/A  Yds/6
Brady,Tom       NE  375 570  66.    4,355   8.     272
Cousins,Kirk    MIN 425 606  70.    4,298   7.     268
Goff,jared      LAR 364 561  65.    4,688   8.     293
Luck,Andrew     IND 430 639  67.    4,593   7.     287
Mahomes,Patrick KC  383 580  66.0   5,097   9.     318
Manning,Eli     NYG 380 576  66.0   4,299   7.     268
Rivers,Philip   LAC 347 508  68.    4,308   8.     269
Rodgers,Aaron   GB  372 597  62.    4,442   7.     277
Roethlisberger, PIT 452  675 67.0   5,129   7.60    320
Ryan,Matt       ATL 422 608  69.    4,924   8.10    307

Receivers                           
Player              Team    Rec  Yds   Yds/R    Yds/6       
Adams,Davante        G8     111 1,386   13.      87.        
Cooks,Brandin       LAR     80  1,204   15.      75.        
Evans,Mike           TB     86  1,524   18.      95.        
Hill,Tyreek          KC     87  1,479   17.0    92.     
Hopkins,DeAndre      HOU    115 1,572   14.     98.     
lonesOullo           ATL    113 1,677   15.     105.        
Smith-Schuster,JuJu PIT     111 1,426   13.     89.     
Thielen,Adam        KIN     113 1,373   12.     86.     
Thomas,Michael      NO      125 1,405   11.     88.     
Woods,Robert        LAR     86  1,219   14.     76.     

你能更清楚一点,解释一下你面临的问题是什么吗?你有什么错误吗?还是输出不一样?

我要冒险了,想知道你是否遇到了 java.util.MissingFormatArgumentException 例外?

编辑:

好的,这是有原因的 - 您的格式设置不正确。这是另一个问题的重复:

System.out.printf("%-25s %-5s %12.0f %12.0f %12.0f %12.1f %12.2f",
                        quarterback.getFirstName() + " " + quarterback.getLastName(), quarterback.getTeam(), (double)quarterback.getCompletions(), (double)quarterback.getAttempts(), (double)quarterback.getYards(), quarterback.yardsPerAttempt(), quarterback.yardsPerGame());
System.out.println("/n");

但是,我强烈建议您阅读有关使用说明符进行格式化的内容。您收到该异常的原因是您没有将变量正确关联到说明符。