尝试编译这个,但无法弄清楚错误。几天来一直困扰着这个。是的,我是新手......任何人都可以帮助我吗? 获取代码底部方法中的错误。
import java.util.ArrayList;
import java.util.Scanner;
public class BankLogic
{
private long pNr;
private int accountId;
private double amount;
private double balance;
private double rate;
private ArrayList<Customer> customerlist;
private ArrayList<SavingsAccount> accounts;
Scanner in = new Scanner(System.in);
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public BankLogic(){
customerlist = new ArrayList<Customer>();
accounts = new ArrayList<SavingsAccount>();
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String toString(){
String info = "Personnummer: " + pNr + "\nKontonummer: " + accountId
+ "\nSaldo: " + amount;
}
//----------------------------------------------------------------------
// Beskrivning: returnerar presentation av alla kunder(pers.nr och namn)
// Returvärde: String
//----------------------------------------------------------------------
public String infoBank(){
return customerlist.toString();
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean addCustomer(String name, long pNr){
for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
return false;
else
{
Customer aCust = new Customer(name, pNr);
customerlist.add(aCust);
}
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String infoCustomer(long pNr){
for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
{
System.out.println(a);
}
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean changeCustomerName(String name, long pNr){
for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
{
Customer aCust = new Customer(name, pNr);
customerlist.add(aCust);
}
else
return false;
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String removeCustomer(long pNr){
for (int i = 0; i < customerlist.length; i++)
{
Customer a = customerlist.get(i);
if (a.getPCode() == pNr)
{
customerlist.remove(i);
}
else
i++;
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public int addSavingsAccount(long pNr){
for (Customer a : customerlist)
{
if (a.getPCode() == pNr)
{
boolean added = false;
for (int i = 0; !added && i < accounts.size(); i++)
{
added = accounts.get(i).addAccount(a);
if(added)
{
System.out.println("Kontonummer: " + accounts.get(i).getAccountId();
}
else
return "Kontot skapades inte.";
}
}
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public String infoAccount(long pNr, int accountId)
{
for (Customer a : customerlist)
{
if (a.getAccountId() == accountId && a.getPCode() == pNr)
return a.infoCust();
}
return null;
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean deposit(long pNr, int accountId, double amount){
for (Customer a : customerlist)
{
if ((a.SavingsAccount.getAccountId() == accountId) && (a.getPCode() == pNr))
{
a.SavingsAccount.getBalance() = balance + amount;
}
else
return false;
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
public boolean withdraw(long pNr, int accountId, double amount){
for (Customer a : customerlist)
{
if ((a.getAccountId() == accountId) && (a.getPCode() == pNr))
{
a.getBalance() = balance - amount;
return true;
}
else
return false;
}
}
}
答案 0 :(得分:1)
上面的代码中有各种各样的错误,我一眼就能看到:
toString()
没有返回任何内容(它只声明一个局部变量,然后在方法结束时立即进行垃圾回收)addCustomer()
并不会总是返回一些内容。infoCustomer()
和removeCustomer()
永远不会返回任何内容,尽管声明它们应该在方法标题中addSavingsAccount()
应该返回一个int(根据方法标题),但只返回一个字符串 - 有时候。Customer
有一个名为SavingsAccount
的公共字段(从很多方面来看设计很糟糕),然后a.SavingsAccount.getAccountId()
和类似的错误,你可能想要Customer
上的方法1}}获得其特定的储蓄账户。您需要养成以更小的增量检查代码的习惯,至少在您掌握基本的语法方面之前。这样你就不会陷入这种情况,你需要解决大量的错误,而不知道如何修复错误。
每次添加(例如)一个方法:停止,编译,检查它是否按照您的意愿工作,然后才转到下一个方法。从长远来看,它会为你节省很多时间。
答案 1 :(得分:0)
以下两个陈述不正确。表达式的左侧必须是变量,在您的情况下,它是一个值。
a.SavingsAccount.getBalance() = balance + amount;
a.getBalance() = balance - amount;
使用适当的变量或setter方法纠正它们。希望这会有所帮助。