【发布时间】:2022-11-22 12:40:02
【问题描述】:
我正在用 Java 编写一个程序,我做了两个类。在第二个中,我制作了一个数组,但我不能在其中放置任何参数。
`
public class Teams {
String Name;
private String Country;
private int Score;
public Teams(String Name, String Country, int Score) {
this.Name = Name;
this.Country = Country;
this.Score = Score;
}
public String getCountry() {
return Country;
}
public int getScore() {
return Score;
}
@Override
public String toString(){
return "Team Name: " + Name + " Team Country: " + Country + " Team Score: " + Score + "."; }
}
`
那是我的第一堂课 现在这是第二堂课
`
public class TD {
public void enterTeam(int N){
Teams[] team = new Teams(String Name, String Country, int Score);
Scanner t = new Scanner(System.in);
for (int i=0; i<N; i++) {
System.out.println("Enter a name for your team: ");
Name = t.nextLine();
System.out.println("\nEnter the country of origin: ");
Country = t.nextLine();
System.out.println("\n Enter a score for the team: ");
Score = t.nextInt();
TeamNumber++;
}
}
`
我试图在数组中放置一些参数,但它不接受它们,我的 ide 一直给我这个错误
constructor Teams() cannot be applied to given types required: String, String, int found: no arguments
【问题讨论】:
-
题外话:Java 命名约定有以小写字母开头的变量名:
name、country、score、n。 -
你可能想要
Teams[] team = new Teams[N]。在您的循环中,在用户输入详细信息后,输入team[i] = new Teams (Name, Country, Score);。