未找到Java Main方法

x33g5p2x  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(196)

我收到“错误:在类Assign5中未找到Main方法,请将main方法定义为:public static void main(String[] args)或JavaFX应用程序类必须扩展javafx.application.Application”消息。我在页面上没有任何错误,我不确定下一步该怎么做。

import java.util.HashMap;

public class Assign5 {

  private String gamePlayed;
  private HashMap < String, Integer > players;

  public Assign5() {
    gamePlayed = "";
    players = new HashMap < String, Integer > ();
  }
  
  public Assign5(String game) {
    this();
    setGame(game);
  }

  public void addName(String name) {
    players.put(name, 0);
  }

  public String getPlayerName(int playerNumber) {
    int i = 1;
    for (String name: players.keySet()) {
      if (i == playerNumber) {
        return name;
      }
      i++;
    }
    return null;
  }

  public void setGame(String game) {
    gamePlayed = game;
  }

  public String getGame() {
    return gamePlayed;
  }

  public int addScore(String playerName, int points) {
    int currentScore = players.get(playerName);
    currentScore += points;
    players.put(playerName, currentScore);
    return currentScore;
  }

  public int subScore(String playerName, int points) {
    int currentScore = players.get(playerName);
    currentScore -= points;
    players.put(playerName, currentScore);
    return currentScore;
  }

  public void listAllScores() {
    System.out.println("Game: " + gamePlayed);
    for (String name: players.keySet()) {
      System.out.println(name + ": " + players.get(name));
    }
  }

}
//******************* Section 2

 class Baseball extends Assign5 {
  private int fouls;
  private int balls;
  private int strikes;
  private int outs;

  private double inning;

  public Baseball() {
    super(); 
    fouls = 0;
    balls = 0;
    strikes = 0;
    outs = 0;
    inning = 1.0;
  }
  public Baseball(String homeTeam, String visitingTeam) {
    super();
    setGame(homeTeam + " vs " + visitingTeam);
    addName(homeTeam);
    addName(visitingTeam);
    fouls = 0;
    balls = 0;
    strikes = 0;
    outs = 0;
    inning = 1.0;
  }
  public void advOuts() {
    outs++;
    if (outs == 3) {
      fouls = 0;
      balls = 0;
      strikes = 0;
      outs = 0;
      inning += 0.5;
    }
  }
  public int getOuts() {
    return outs;
  }
  public void advStrikes() {
    strikes++;
    if (strikes == 3) {
      advOuts();
    }
  }
  public int getStrikes() {
    return strikes;
  }
  public void advFouls() {
    fouls++;
    if (strikes < 2) {
      strikes++;
    }
  }
  public int getFouls() {
    return fouls;
  }

  public void advBalls() {
    balls++;
    if (balls == 4) {
      fouls = 0;
      balls = 0;
      strikes = 0;
    }
  }

  public int getBalls() {
    return balls;
  }
  
  public double getInning() {
    return inning;
  }
 
 
// Section 3: Testing Base Class
  {
System.out.println("Assessment 10 - Classes and Inheritance\n");
System.out.println("Section 3: Base Class Results After Adding\n");

Assign5 gameOne = new Assign5("Canasta");

gameOne.addName("Larry");
gameOne.addName("Moe");
gameOne.addName("Curly");

System.out.println();

gameOne.addScore("Larry", 20);
gameOne.addScore("Moe", 35);
gameOne.addScore("Curly", 45);
gameOne.listAllScores();

gameOne.subScore("Moe", 15);
gameOne.subScore("Curly", 5);
System.out.println("\nSection 3: Base Class Results After Subtracting\n");
gameOne.listAllScores();

System.out.println("\n\n");

 }
public class BaseballTester {
  public static void main(String[] args) {
    System.out.println("Section 4: Derived Class Results: Baseball scoring\n");
    Baseball gameTwo = new Baseball("Cubs", "Braves");

    gameTwo.addScore("Cubs", 2);
    gameTwo.advOuts();
    gameTwo.advOuts();
    gameTwo.advOuts();
    gameTwo.addScore("Braves", 3);
    gameTwo.advOuts();
    gameTwo.advStrikes();
    gameTwo.advFouls();
    gameTwo.advFouls();
    gameTwo.advFouls();
    gameTwo.advBalls();
    gameTwo.listAllScores();
   
    
    System.out.println();
    System.out.println("Current Inning: " + gameTwo.getInning());
    System.out.println("Current Outs: " + gameTwo.getOuts());
    System.out.println("Current Strikes: " + gameTwo.getStrikes());
    System.out.println("Current Fouls: " + gameTwo.getFouls());
    System.out.println("Current Balls: " + gameTwo.getBalls());
  }
}
}

我知道我需要添加一个“public static void main(String[] args)”,但是我不知道该把它放在哪里。我不断地收到更多的错误,还有我应该把右括号放在哪里?

ukxgm1gy

ukxgm1gy1#

Main方法无法从内部类运行,将BaseballTester类从Baseball类中取出,最好每个类都有自己的java文件。

// Assign5.java
public class Assign5 { ... }

// Baseball.java
public class Baseball extends Assign5 { ... }

// BaseballTester.java
public class BaseballTester {
    public static void main(String[] args) {
        ...
    }
}

相关问题