【问题标题】:Object Array in Java does not take argumentsJava 中的对象数组不带参数
【发布时间】: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 命名约定有以小写字母开头的变量名:namecountryscoren
  • 你可能想要Teams[] team = new Teams[N]。在您的循环中,在用户输入详细信息后,输入team[i] = new Teams (Name, Country, Score);

标签: java arrays netbeans


【解决方案1】:

这一行:

Teams[] team = new Teams(String Name, String Country, int Score);

您声明了一个数组,但初始化了一个 Teams 对象。将新对象的首字母更改为数组的首字母,并在循环中为数组创建一个新的 Teams 对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2019-07-16
    相关资源
    最近更新 更多