gpt4 book ai didi

java - 使用 JOptionPane 进行试验。执行过程中出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 09:26:55 25 4
gpt4 key购买 nike

我想在存入一些钱后取出一些钱,但我得到的是一个错误..为什么?例如,我存入100,然后我想取出90,但我得到的只是一个错误。另一个是,存入 100 后,我想打印我的当前余额,但它打印出 (0) 零。为什么?请帮忙。

主类

import java.awt.HeadlessException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class Banko{

private static String name;
private static double bal;
private static double withdraw;
private static BankAccount myAccount;

public static void main(String args[]) {

name = JOptionPane.showInputDialog(null, "Enter your name: ");

String num;
int pin;
num = JOptionPane.showInputDialog("Enter your pin number: ");
pin = Integer.parseInt(num);

JOptionPane.showMessageDialog(null, "Login Success\n" + "Name : " + name + "\n" + "Pin Number : " + pin);

BankAccount myAccount = new BankAccount(withdraw, name);

int rc = getRC();
processor(num, rc);
}

private static int getRC(){
String[] buttons = { "Deposit", "Withdraw", "Print Balance", "Exit" };
int rc = JOptionPane.showOptionDialog(
null,
"What would you like to do?",
"Confirmation",
JOptionPane.INFORMATION_MESSAGE,
0,
null,
buttons,
buttons[2]);
return rc;
}

private static void processor(String num, int rc) {
switch(rc) {
case 0:
processDeposit(num, rc);
break;
case 1:
processWithdraw(num, myAccount, rc);
break;
case 2:
processBalance(num, rc);
default:
processExit(rc);
break;
}
}

private static void processExit(int rc) throws HeadlessException {
if(rc == -1) {
JOptionPane.showMessageDialog(null, "\nThank you. Have a good day!");
System.exit(0);
}
}


private static void processDeposit(String num, int rc) throws HeadlessException, NumberFormatException {
int deposit;
String dep = JOptionPane.showInputDialog("How much would you like to deposit?\n\t$ ");
deposit = Integer.parseInt(num);
JOptionPane.showMessageDialog(null, "You have deposited $" + dep + " into the account of " + name);

String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
if(proceeds.equalsIgnoreCase("y")) {
rc = getRC();
processor(num, rc);
} else {
processExit(-1);
}
}


private static void processBalance(String num, int rc) throws HeadlessException {
JOptionPane.showMessageDialog(null, "The balance in the account of " + name + " with the pin number " + num
+ " is $" + bal);

String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
if(proceeds.equalsIgnoreCase("y")) {
rc = getRC();
processor(num, rc);
} else {
processExit(-1);
}
}



private static void processWithdraw(String num, BankAccount myAccount, int rc) throws HeadlessException, NumberFormatException {
double withdraw;
String with = JOptionPane.showInputDialog("How much would you like to withdraw?\n\t$");
withdraw = Integer.parseInt(num);
if(bal - withdraw > 0) {
myAccount.withdraw(withdraw);
JOptionPane.showMessageDialog(null, "You have withdrawn $" + withdraw + " from the account of " + name
+ ". The new balance is: " + myAccount.getBalance());
} else {
JOptionPane.showMessageDialog(
null,
"Sorry, you have insufficient funds for this operation. Your existing balance is $"
+ myAccount.getBalance());
}

String proceeds = JOptionPane.showInputDialog(null, "\nWould you like to do another transaction? (Y/N)");
if(proceeds.equalsIgnoreCase("y")) {
rc = getRC();
processor(num, rc);
} else {
processExit(-1);
}

}
}

基类:

import java.util.Scanner;

public class BankAccount {

private double balance;

private String name;

public BankAccount(double b, String n) {
this.balance = b;
this.name = n;
}

public void deposit(double d) {
balance += d;
}

public void withdraw(double w) {
balance -= w;
}

public String nickname() {
System.out.print("Enter a new name: ");
Scanner kbIn = new Scanner(System.in);
String n = kbIn.nextLine();
return n;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

最佳答案

很好的实验。只有小错误。

  1. 不必要的变量 bal 因为您的账户本身有余额
  2. 必须在所有地方使用对象 myAccount

存款的变化

    deposit = Integer.parseInt(dep);
JOptionPane.showMessageDialog(null, "You have deposited $"
+ dep + " into the account of " + name);
myAccount.setBalance(myAccount.getBalance() + deposit);

退出

    withdraw = Integer.parseInt(with);
if (myAccount.getBalance() - withdraw > 0) {

获取余额

 JOptionPane.showMessageDialog(null, "The balance in the account of " 
+ name + " with the pin number " + num
+ " is $" + myAccount.getBalance());

关于java - 使用 JOptionPane 进行试验。执行过程中出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799200/

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