【问题标题】:Where to add main method when it says class does not have a static void main method accepting String[]当它说类没有接受 String[] 的静态 void 主方法时在哪里添加主方法
【发布时间】:2018-07-22 18:17:22
【问题描述】:

我正在尝试让我的名为 ZawTennisPlayer 的程序使用构造函数和显示方法输出以下内容。 我正在使用名为 TestTennisPlayer2 的示例代码对其进行测试并获得所需的输出。 我当前的代码是:

public class ZawTennisPlayer

{
//instance variables
private String playerName;
private String country;
private int rank;
private int age;
private int wins;
private int losses;

//default constructor
public ZawTennisPlayer()
{
playerName=null;
country=null;
rank=0;
age=0;
wins=0;
losses=0;

}

//parameterized constructor
public ZawTennisPlayer(String playerName,String country)
{
this.playerName=playerName;
this.country=country;
rank=0;
age=0;
wins=0;
losses=0;

}

//parameterized constructor
public ZawTennisPlayer(String playerName,String country,int rank, int age)
{
this.playerName=playerName;
this.country=country;
this.rank=rank;
this.age=age;
wins=0;
losses=0;
}

//parameterized constructor
public ZawTennisPlayer(String playerName,String country,int rank, int age,int wins,int losses)
{
this.playerName=playerName;
this.country=country;
this.rank=rank;
this.age=age;
this.wins=wins;
this.losses=losses;

}

//all accesor and mutator method for all six fields.

public String getPlayerName()

{

return playerName;

}

public void setPlayerName(String playerName)

{

this.playerName = playerName;

}

public String getCountry()

{

return country;

}

public void setCountry(String country)

{

this.country = country;

}

public int getRank()

{

return rank;

}

public void setRank(int rank)

{

this.rank = rank;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public int getWins()

{

return wins;

}

public void setWins(int wins)

{

this.wins = wins;

}

public int getLosses()

{

return losses;

}

public void setLosses(int losses)

{

this.losses = losses;

}

//method to display player details
public void displayPlayer()
{

System.out.println("Player's name: " + getPlayerName());

System.out.println("Player's country: " + getCountry());

System.out.println("Player's rank: " + getRank());

System.out.println("Player's age: " + getAge());

System.out.println("Player's wins: " + getWins());

System.out.println("Player's losses: " + getLosses());

System.out.println();
}

}

但是,当此程序编译时,我收到“此类没有接受 String[] 的静态 void main 方法”的错误。当我运行它时。我知道我应该在 ZawTennisPlayer 程序的某处添加一些“public static void main(String[] args)”,但我不确定在哪里。知道如何修复程序以获得所需的输出吗?提前致谢!

我用来测试程序的示例代码“TestTennisPlayer2”是:

public class TestTennisPlayer2
{
  public static void main(String[] args)
  {
    ZawTennisPlayer tp1 = new ZawTennisPlayer();
    ZawTennisPlayer tp2 = new ZawTennisPlayer("Nick Kyrgios", "Australia");
    ZawTennisPlayer tp3 = new ZawTennisPlayer("Simona Halep", "Romania", 1, 26);
    ZawTennisPlayer tp4 = new ZawTennisPlayer("Novak Djokovic", "Serbia", 18, 30, 6, 6);

    tp1.displayPlayer();
    tp2.displayPlayer();
    tp3.displayPlayer();
    tp4.displayPlayer();




  }
}

【问题讨论】:

  • 问题不在于ZawTennisPlayer 没有public static void main 方法,而在于您正在尝试运行它。不。编译它,编译TestTennisPlayer2,然后只尝试运行TestTennisPlayer2
  • 也请阅读minimal reproducible example。不需要这些屏幕截图!相反,花时间合理地格式化/缩进你的代码示例。

标签: java constructor display


【解决方案1】:

如 cmets 中所述,您似乎正在尝试运行 ZawTennisPlayer,而您应该运行 TestTennisPlayer2
如果您通过命令提示符运行,请使用

>javac TestTennisPlayer2.java

>java TestTennisPlayer2 

或来自 Eclipse IDE,

open TestTennisPlayer2.java, right click -> run as > java application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多