Java 我能';无法识别此错误消息(ArrayList银行帐户代码)

Java 我能';无法识别此错误消息(ArrayList银行帐户代码),java,arraylist,Java,Arraylist,我正试图为明天的Java测试进行练习,但我不知道到底发生了什么 import java.util.ArrayList; public class BankAccount { public static void BankAccount(double x) { ArrayList<BankAccount> accounts = new ArrayList<BankAccount>(); BankAccount ted = ne

我正试图为明天的Java测试进行练习,但我不知道到底发生了什么

import java.util.ArrayList;
public class BankAccount
{
    public static void BankAccount(double x)
    {
        ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
        BankAccount ted = new BankAccount(400);
        BankAcconut bob = new BankAccount(300);
        BackAccount carol = new BankAccount(500);
        accounts.add(new BankAccount(300));
    }
}
import java.util.ArrayList;
公营银行帐户
{
公共静态无效银行账户(双x)
{
ArrayList accounts=新的ArrayList();
银行账户ted=新银行账户(400);
BankAccount bob=新银行账户(300);
后台账户=新银行账户(500);
增加(新银行账户(300));
}
}
我一直收到一条错误消息,上面说“BankAccount类中的构造函数BankAccount无法应用于给定的术语;必需:无参数;找到:int


我知道这和你们现在做的相比是小菜一碟,但我是新手。提前谢谢。

你们没有提供任何构造函数,所以,没有参数,这没有任何作用


提供一个单参数构造函数,该构造函数接受您提供的
int

您的类
BankAccount
需要一个如下所示的构造函数

public BankAccount(int funds){
    this.funds = funds;
}
这假设类
BankAccount
具有一个名为
funds
的属性,该属性可以表示(例如)可用资金

public static void BankAccount(double x)
Java中没有静态构造函数。这就是它没有被重新定义为构造函数的原因。只需删除
static
void
,如下所述


因为没有自动定义的构造函数,所以在编译时添加了一个没有参数的构造函数,并且因为“您的“构造函数与您要调用的构造函数不匹配。如果遇到此错误,请执行以下操作。”

对于
BankAccount
,您没有任何显式构造函数。您似乎将名为
BankAccount
(不要这样做;这会让您感到困惑)的
静态
方法与构造函数混淆。你可能想做这样的事情:

public class BankAccount {
    private double balance;

    public BankAccount(double balance) {
        this.balance = balance;
    }
}

然后将正在构建
BankAccount
对象的其他代码放到其他地方,比如
main
。(将
main
放在同一个类上是可以的,但它的位置基本上是不相关的。重要的是,您需要为您试图构造的类使用构造函数。)

发生这种情况是因为您没有定义接受
int
BankAccount
构造函数。按如下方式修改您的代码:

import java.util.ArrayList;
public class BankAccount
{
    // public constructor of BankAccount class
    public BankAccount(int value){
        // do something here with value
    }
    public static void BankAccount(double x)
    {
        ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
        BankAccount ted = new BankAccount(400);
        BankAcconut bob = new BankAccount(300);
        BackAccount carol = new BankAccount(500);
        accounts.add(new BankAccount(300));
    }
}
import java.util.ArrayList;
公营银行帐户
{
//BankAccount类的公共构造函数
公共银行账户(整型值){
//在这里做一些有价值的事情
}
公共静态无效银行账户(双x)
{
ArrayList accounts=新的ArrayList();
银行账户ted=新银行账户(400);
BankAccount bob=新银行账户(300);
后台账户=新银行账户(500);
增加(新银行账户(300));
}
}
在数字后添加“D”;这将使它成为一个双重的

那么这个

银行账户ted=新银行账户(400)

变成

银行账户ted=新银行账户(400D)

import java.util.ArrayList;
公营银行帐户{
私人双余额=0.0;
公共银行账户(){
//TODO自动生成的构造函数存根
}
公共银行账户(双d){
//TODO自动生成的构造函数存根
平衡=d;
}
公共静态void main(字符串[]args)//BankAccount(双x)
{
ArrayList accounts=新的ArrayList();
银行账户ted=新银行账户(400.0);
银行账户bob=新银行账户(300.0);
银行账户=新银行账户(500);
增加(新银行账户(300));
}
}

错误很明显。您的类中没有接受
int
的构造函数。请再次查看错误消息。代码中自动包含默认的无参数构造函数。没有int构造函数。因此出现了错误。一个与类同名的静态方法是个坏主意。另外,参数
x
的要点是什么;它从未被使用过……还有虚空。而且,这个方法不应该是构造函数,否则会有一个构造函数,请考虑一下。哇!我去了洗手间,回来发现已经有7个人帮了我!我对你感激不尽。我的代码现在工作得很好。希望我没有给人以愚蠢的印象
import java.util.ArrayList;


public class BankAccount {

private double balance = 0.0;

public BankAccount() {
    // TODO Auto-generated constructor stub

}

public BankAccount(double d) {
    // TODO Auto-generated constructor stub
    balance = d;
}

public static void main(String[] args) //BankAccount(double x)
{
    ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
    BankAccount ted = new BankAccount(400.0);
    BankAccount bob = new BankAccount(300.0);
    BankAccount carol = new BankAccount(500);
    accounts.add(new BankAccount(300));
}

}