编译错误Java

时间:2014-01-30 23:36:59

标签: java compiler-errors

当我尝试编译此代码时,我不断提出编译错误。代码如下。

import java.util.Scanner;

public class BaseballPitcher
{
    private final int MAX_GAMES = 15;
    private gamesPitched[];
    private int totalGames;
    private int totalRuns;
    private float totalInnings;
    private double ERA;

    public BaseballPitcher()
    {
        gamesPitched = new GamePitched[];
    }
    public void inputGameData()
    {
        Scanner input = new Scanner (System.in);

        do
        {
            System.out.printf ("How many games will you enter (up to %d): ", MAX_GAMES);
            totalGames = input.nextInt();

            if (totalGames <= 0 || totalGames > MAX_GAMES)
            {
                System.out.printf ("Error: You may only enter up to %d games" + " - Please try again\n\n", MAX_GAMES);
            }
         } while (totalGames <= 0 || totalGames > MAX_GAMES);

         for (int x = 0, x < totalGames, x++)
         {
             gamesPitched[x] = new GamePitched();
             gamesPitched[x].inputGame();
         }
    }
}

我收到的编译错误是:

[Please list your compile error or errors].

为什么我会收到编译错误?

2 个答案:

答案 0 :(得分:4)

您似乎有一个名为GamePitched的自定义类。您的数组gamesPitched[]可能应该是此类型的数组,但您已声明它没有类型。试试private GamePitched[] gamesPitched;

你的循环只有语法错误 - ,应该是;,Java用它来分隔for循环中的子句。

您应该阅读stacktrace中提供的错误。即使您是Java新手,它们也具有相当的描述性,可以帮助您确定存在问题的位置(通常建议修复)。

答案 1 :(得分:0)

嗯,这就是它应该的样子,我想

import java.util.Scanner;

public class BaseballPitcher
{
    private final int MAX_GAMES = 15;
    private GamePitched[] gamesPitched;
    private int totalGames;
    private int totalRuns;
    private float totalInnings;
    private double ERA;

    public BaseballPitcher()
    {
        gamesPitched = new GamePitched[10];
    }
    public void inputGameData()
    {
        Scanner input = new Scanner (System.in);

        do
        {
            System.out.printf ("How many games will you enter (up to %d): ", MAX_GAMES);
            totalGames = input.nextInt();

            if (totalGames <= 0 || totalGames > MAX_GAMES)
            {
                System.out.printf ("Error: You may only enter up to %d games" + " - Please try again\n\n", MAX_GAMES);
            }
         } while (totalGames <= 0 || totalGames > MAX_GAMES);

         for (int x = 0; x < totalGames; x++)
         {
             gamesPitched[x] = new GamePitched();
             gamesPitched[x].inputGame();
         }
    }
}

至少要编译。什么都不能说逻辑

,另一个类必须至少有这个方法

public class GamePitched {

    public void inputGame() {
        // TODO Auto-generated method stub

    }

}