Java构造函数帮助
请看//合并部分。我想做的是,将acct1和acct2合并到一个新账户中,限制条件是acct1和acct2必须具有相同的名称,acct1和acct2账号必须彼此不同,如果满足这些条件,则使用两个旧账户的新余额创建一个新账户,保持相同的名称,并随机生成一个新帐号。我的代码中缺少什么吗?它不会编译。这些都是我犯的错误Java构造函数帮助,java,constructor,Java,Constructor,请看//合并部分。我想做的是,将acct1和acct2合并到一个新账户中,限制条件是acct1和acct2必须具有相同的名称,acct1和acct2账号必须彼此不同,如果满足这些条件,则使用两个旧账户的新余额创建一个新账户,保持相同的名称,并随机生成一个新帐号。我的代码中缺少什么吗?它不会编译。这些都是我犯的错误 //******************************************************* // Account.java // // A bank accou
//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Random;
public class Account
{
private double balance;
private String name;
private long acctNum;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
//----------------
//Track how many accounts
//----------------
private static int numAccounts=0;
{
numAccounts++;
}
public static int getNumAccounts()
{
return numAccounts;
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
// Get name of account
public String getName()
{
return name;
}
//----------------------------------------------
// Returns account number.
//----------------------------------------------
public long getAcctNumber()
{
return acctNum;
}
//----------------
//Void and close the accounts
//----------------
public void close()
{
balance = 0;
name += "CLOSE";
numAccounts--;
}
//----------------
//Consolidating accounts
//----------------
public static Account consolidate(Account acct1,Account acct2)
{ Account newAccount=null;
if((acct1.getName()).equals(acct2.getName()))
if(acct1.getAcctNumber()!=acct2.getAcctNumber())
{newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner);
Random generator = new Random();
acctNum= generator.nextInt();
acct1.close();
acct2.close();
}
else
System.out.println("Not allow,same account number");
else
System.out.println("Can't use other people account");
return newAccount;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name: " + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}
Account.java:95:“)”应为
{newAccount=newAccount(acct1.getBalance()+acct2.getBalance(),字符串所有者);
^
Account.java:95:表达式的非法开始
{newAccount=newAccount(acct1.getBalance()+acct2.getBalance(),字符串所有者);
^
字符串所有者
应该是acct1.getName()
或任何检索名称的函数
此外,行acctNum=generator.nextInt();
将失败,因为该上下文中未定义acctNum
。此外,您没有将newAccount
的帐号设置为此acctNum
变量
我建议您将其更改为:
newAccount.setAcctNumber(generator.nextInt());
字符串所有者应为acct1.getName()
或任何检索名称的函数
此外,行acctNum=generator.nextInt();
将失败,因为该上下文中未定义acctNum
。此外,您没有将newAccount
的帐号设置为此acctNum
变量
我建议您将其更改为:
newAccount.setAcctNumber(generator.nextInt());
第行的编译错误
newAccount=newAccount(acct1.getBalance()+acct2.getBalance(),字符串所有者)
这是因为字符串所有者
是一个声明,应该在方法签名中使用,就像上面所做的那样。但是,当您实际调用构造函数时,需要在中发送字符串
参数
编译器正在抱怨,因为您在这一行实际执行的是声明一个名为owner
的String
变量。Java不允许在方法调用中使用该变量
Finbarr是对的;请使用该方法获取帐户名。第行的编译错误
newAccount=newAccount(acct1.getBalance()+acct2.getBalance(),字符串所有者)
这是因为字符串所有者
是一个声明,应该在方法签名中使用,就像上面所做的那样。但是,当您实际调用构造函数时,需要在中发送字符串
参数
编译器正在抱怨,因为您在这一行实际执行的是声明一个名为owner
的String
变量。Java不允许在方法调用中使用该变量
Finbarr是对的;请使用该方法获取帐户名称。下次请将您的问题标记为家庭作业,如果是(这次我会这样做),并格式化您的代码,使其可读(使用文本框上方的按钮)。这看起来非常类似(而且,就这一点而言,)。你可以选择一个进行更新编辑,将内容保留在一个地方吗?是的,它是同一个,对不起,我不知道我应该编辑旧的还是下一次制作新的。如果是,请将你的问题标记为家庭作业(这次我会这样做),并格式化你的代码,使其可读(使用文本框上方的按钮)。这看起来非常类似于(就此而言,)。您可以选择一个进行编辑并进行更新,以便将内容保留在一个位置吗?是的,它是同一个,抱歉,我不知道如果我使用newAccount.setAcctNumber(generator.nextInt()),是编辑旧的还是创建新的;那么我是否需要在构造函数中的某些位置也创建一个setAcctNumber?不是在构造函数中-而是在Account
类的主体中声明一个带有签名的方法void setAcctNumber(int number){this.acctNumber=number;}
如果我创建newAccount.setAcctNumber(generator.nextInt());那么我是否需要在构造函数中的某些位置也设置setAcctNumber?不是在构造函数中-而是在帐户
类的主体中声明一个带有签名的方法void setAcctNumber(int number){this.acctNumber=number;}
如果我想为新合并账户生成一个随机数呢?我不知道如何在该部分中执行该操作如果我想为新合并账户生成一个随机数呢?我不知道如何在该部分中执行该操作
Account.java:95: ')' expected
{newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner);
^
Account.java:95: illegal start of expression
{newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner);
^