gpt4 book ai didi

java - 银行账户计划。空指针异常错误

转载 作者:行者123 更新时间:2023-12-01 11:45:27 24 4
gpt4 key购买 nike

我正在为我的 Java 类(class)编写一个银行帐户程序。我对此还很陌生,这是我们第一个使用数组的作业。所以请原谅任何菜鸟错误,哈哈。目前,该作业需要 3 个银行帐户的数组。我需要3节课; Bank(包含bankAcct myAcct[] = newbankAcct)、bankUser 和bankAcct(包含我的提款、存款和查看余额方法)。截至目前,每当我尝试查看余额、存款或提款时,我都会在 proChoice 方法中收到 NullPointerException。错误出现在我的 User 类中,我将在代码中留下注释以显示具体位置。请您提供任何帮助,我们将不胜感激。我正在阅读我的教科书,但并没有真正发现太多可以帮助我解决特定问题的内容。预先感谢您。

bankAcct 类

import java.util.Scanner;

public class bankAcct
{

private double Bal;
private int acctNum;
private String name;

Scanner scannerObject = new Scanner(System.in);

public bankAcct(int pacctNum, double pBal, String pname) {
pBal = Bal;
pacctNum = acctNum;
pname = name;
}


public void makeDeposit()
{
System.out.print( "Hello " + name + ", please, enter amount to deposit $");
double lDep;
lDep = scannerObject.nextDouble();
Bal = Bal + lDep;
System.out.println( " You have deposited $" + lDep);
System.out.println( " Your new balance is $" + Bal);
}

public void makeWithdrawal()
{
System.out.print( "Hello " + name + ", please, enter amount to withdraw $");
double lWDraw;
lWDraw = scannerObject.nextDouble();
if (lWDraw <= Bal){
Bal = Bal - lWDraw;
System.out.println( "You have withdrawn $" + lWDraw);
System.out.println( "Your new balance is $" + Bal);
}else{
System.out.println("Insufficient funds!");
}
}

public void dispBal()
{
System.out.println( "Your current balance is $" + Bal);
}


public void setAcctNum(int pacctNum)
{
pacctNum = acctNum;
}

public int getAcctNum()
{
return acctNum;
}

public void setName(String pname)
{
pname = name;
}

public String getName()
{
return name;
}


}

银行类别

import java.util.Scanner;

public class Bank
{
int max = 3;
int count;
bankAcct myAcct[] = new bankAcct[max];
bankUser user = new bankUser();
Scanner scannerObject = new Scanner(System.in);

public void openAcct()
{
String lname;
if (count >= max){
System.out.println("Not accepting new customers at this time.");
}else{
System.out.println("Please enter your name: ");
lname = scannerObject.nextLine();
myAcct[count] = new bankAcct(count + 1, 0, lname);
count++;
System.out.println("Thank you " + lname + ", your account number is: " + count);
}

}


public int findAcct()
{
int lnum = -1;
System.out.println("Greetings, please enter your account number: ");
lnum = scannerObject.nextInt();
for(count = 0; count < max; count++){
if (count == lnum)
return count;
}
return lnum;
}

public void seeBal()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].dispBal();
}
}

void Deposit()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].makeDeposit();
}
}

void Withdrawal()
{
int lfound = findAcct();
if (lfound == -1)
{
System.out.println("Error!");
}else{
myAcct[lfound].makeWithdrawal();
}
}
}

用户类别

import java.util.Scanner;

public class bankUser
{
public static void main(String[] args)

{
Bank myBank = new Bank();
Scanner scannerObject = new Scanner(System.in);
int Choice;

do
{
dispMenu();

Choice = getChoice(scannerObject);

proChoice(Choice, myBank); ***//Error occurring here***
}
while (Choice !=0);
}


public static void dispMenu()
{
System.out.println( "|==================================|");
System.out.println( "| TONY'S FIRST NATIONAL BANK |");
System.out.println( "|***********Menu Options***********|");
System.out.println( "|__________________________________|");
System.out.println( "| Press 1 To Open New Account |");
System.out.println( "| Press 2 To View Balance |");
System.out.println( "| Press 3 To Make Deposit |");
System.out.println( "| Press 4 To Make Withdrawal |");
System.out.println( "| Press 0 to Exit |");
System.out.println( "|__________________________________|");
System.out.println( "| Please Make Selection Now... |");
System.out.println( "|==================================|");
}

static int getChoice(Scanner scannerObject)
{
int pChoice, Choice;
pChoice = scannerObject.nextInt();
Choice = pChoice;
return Choice;
}

static void proChoice(int Choice, Bank myBank)
{
switch (Choice)
{
case 1: myBank.openAcct();
break;
case 2: myBank.seeBal(); //***Error Here***
break;
case 3: myBank.Deposit(); //***Error Here***
break;
case 4: myBank.Withdrawal(); //***Error Here***
break;
case 0: System.out.println( "Thank you, come again.");
break;
}
}
}

最佳答案

您有几个问题。我解决了第一个,但它会涉及到第二个和第三个。第一个问题是如何创建帐户。您当前的构造函数是:

    public bankAcct(int pacctNum, double pBal, String pname) 
{
pBal = Bal;
pacctNum = acctNum;
pname = name;
}

应该是这样的:

        public bankAcct(int pacctNum, double pBal, String pname) 
{
Bal = pBal;
acctNum = pacctNum;
name = pname;
}

此处的值已颠倒,这导致您无法为您创建的帐户分配帐号、姓氏和余额。

其次,您的 findAcctseeBalance 方法应如下所示:

    public bankAcct findAcct()
{
bankAcct myBankAcct = null;
System.out.println("Greetings, please enter your account number: ");
int acctNum = scannerObject.nextInt();
//make sure you use myAcct.length to ensure you don't get an "ArrayOutOfBoundsIndex" error.
for(count = 0; count < myAcct.length; count++){
myBankAcct = myAcct[count];
if(myBankAcct.getAcctNum() == acctNum){
return myBankAcct;
}
}
return myBankAcct;
}

public void seeBal()
{
bankAcct lfound = findAcct();
if (lfound == null)
{
System.out.println("Error!");
}else{
lfound.dispBal();
}
}

您最大的问题是您的 findAcct 方法。您从未找到该帐户。这就是你所做的:

    public int findAcct()
{
int lnum = -1;
System.out.println("Greetings, please enter your account number: ");
//you get the users acct #
lnum = scannerObject.nextInt();
for(count = 0; count < max; count++){ //here you iterate, but you never "find" the account
if (count == lnum)
return count; //then you return the "count" rather than the account itself.
}
return lnum;
}

如果您进行我所做的更改,它将执行选择 2,但您将不得不处理一些错误。如果您从这篇文章中吸取了教训,您应该能够解决程序中遇到的其他问题。您应该能够使您的 DepositWithdraw 方法看起来像 seeBalance 方法。

这是我运行的输出

    |==================================|
| TONY'S FIRST NATIONAL BANK |
|***********Menu Options***********|
|__________________________________|
| Press 1 To Open New Account |
| Press 2 To View Balance |
| Press 3 To Make Deposit |
| Press 4 To Make Withdrawal |
| Press 0 to Exit |
|__________________________________|
| Please Make Selection Now... |
|==================================|
1
Please enter your name:
Blaine
Thank you Blaine, your account number is: 1
|==================================|
| TONY'S FIRST NATIONAL BANK |
|***********Menu Options***********|
|__________________________________|
| Press 1 To Open New Account |
| Press 2 To View Balance |
| Press 3 To Make Deposit |
| Press 4 To Make Withdrawal |
| Press 0 to Exit |
|__________________________________|
| Please Make Selection Now... |
|==================================|
2
Greetings, please enter your account number:
1
Your current balance is $0.0
|==================================|
| TONY'S FIRST NATIONAL BANK |
|***********Menu Options***********|
|__________________________________|
| Press 1 To Open New Account |
| Press 2 To View Balance |
| Press 3 To Make Deposit |
| Press 4 To Make Withdrawal |
| Press 0 to Exit |
|__________________________________|
| Please Make Selection Now... |
|==================================|

我希望这有帮助:)

关于java - 银行账户计划。空指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29177913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com