【问题标题】:my dog trithlon program gives errors我的狗铁人三项计划出错
【发布时间】:2025-12-07 12:50:01
【问题描述】:
public class DogTriathlonParticipant {
/**
 * @param args the command line arguments
 */
static {



}
public static void main(String[] args) {



{
    {
    private static int NUM_EVENTS;
    private static int totalCumulativeScore = 0;

    private final string name;
    private final int obedienceScore;
    private final int conformationScore;
    private final int agilityScore;
    private final int total;
    private final double avg;
    private String name;





    public DogTriathlonParticipant (String name, int numEvents, int score1,
            int score2, int score3)
    {
        {

            {
                {

        this.name = name;
        NUM_EVENTS = numEvents;
        obedienceScore = score1;
        conformationScore = score2;
        agilityScore = score3;

        total = obedienceScore + conformationScore + agilityScore;
        avg = (double) total / NUM_EVENTS;
        totalCumulativeScore = totalCumulativeScore + total; 

        {
            {
                {

                }
            }
        }
        {

        public void display()


        {
            System.out.println(name + " participated in " + NUM_EVENTS + " Events and has an average score of " + avg);
            System.out.println( " " + name + " has total score of " + total + " bringing the total cumlative score to " + totalCumulativeScore);

        } 

        public class TestDogs
        {

        }
        public static void main(String[] args)
            {

                {    
            {
        DogTriathlonParticipant dog1 = 
                new DogTriathlonParticipant("Bowser" , 2, 85, 89,0);
        dog1.display();
        {

        }
        DogTriathlonParticipant dog2 = 
                new DogTriathlonParticipant("Rush", 3, 78, 72, 80);
        dog2.display();
        {

        }
        DogTriathlonParticipant dog3 =
                new DogTriathlonParticipant("Ginger", 3, 90, 86, 72);
        dog3.display();
        }
        }
    }
}

它不断给我的名字和字符串以及公共空白提供错误 也许我只是需要一个新的眼光来帮助我找出错误以及为什么他们说它们是非法的 你能弄清楚你能不能也向我解释一下为什么这样我就可以知道为什么? 我正在使用 netbeans,对 java 来说还是比较新的

【问题讨论】:

  • 这段代码是否在一个java页面中?您没有为自定义课程 (DogTriathlonParticipant) 创建新页面吗?
  • 你不能把方法放在其他方法里面。
  • 里面是哪个方法

标签: java error-handling static


【解决方案1】:

如果您的目标是创建 DogTriathlonParticipant 实例并通过显示信息“测试”实例化,您可以首先将您的逻辑分离到不同的 java 类/页面中。

您应该在 netbeans 中为 main 和 DogTriathlonParticipant 类创建新的 Java 类。为此,请单击 Source Packages > new > Java Class。

将该类 DogTriathlonParticipant 命名为如下所示:

public class DogTriathlonParticipant {

//private static int NUM_EVENTS;
//private static int totalCumulativeScore = 0; //this logic is not part of DogTriathlonParticipant.

// you should not put final if you plan to edit the variable.
private String name;
private int obedienceScore;
private int conformationScore;
private int agilityScore;
private int total;
private double avg;
private String name;

public DogTriathlonParticipant (String name, int numEvents, int score1, int score2, int score3) {
    this.name = name;
    // NUM_EVENTS = numEvents; this logic don't belong here
    obedienceScore = score1;
    conformationScore = score2;
    agilityScore = score3;

    total = obedienceScore + conformationScore + agilityScore;
    avg = (double) total / numEvents; // edited here, I think you are trying to get the average of to total by numEvents, in this case, the score1 shoulb be the sum of the events "numEvents".
    //totalCumulativeScore = totalCumulativeScore + total; 
}

public void display()
    {
        System.out.println(name + " participated in " + numEvents+ " Events and has an average score of " + avg);
        System.out.println( " " + name + " has total score of " + total + " bringing the total cumlative score to " + total);
    } 
 }

那么你应该有这样的“测试”类:

public class TestDogs {
    public static void main(String[] args) {

        DogTriathlonParticipant dog1 = new DogTriathlonParticipant("Bowser" , 2, 85, 89,0);
        DogTriathlonParticipant dog2 = new DogTriathlonParticipant("Rush", 3, 78, 72, 80);
        DogTriathlonParticipant dog3 = new DogTriathlonParticipant("Ginger", 3, 90, 86, 72);

        dog1.display();
        dog2.display();
        dog3.display();
    }
}

有了这个,你应该有一个很好的基础来开始和添加你的逻辑并纠正那个地方。

【讨论】: