gpt4 book ai didi

Java 程序无法正确添加金额

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

所以我对java还很陌生。我已经写了这个,但不确定为什么当我运行它时,金额不会添加到客户端中设置的内容。

例如:我输入 500 作为起始余额,如果我点击存款,然后输入 500,然后点击案例 3,它应该显示 1000,但它仍然显示 500。然后如果我点击提款,它会显示我 - 500。

有什么想法吗?谢谢

package bankaccount;
import java.util.Random;
import java.util.Scanner;

public class Client {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your Name: ");
String cusName = input.nextLine();
Random randomGenerator = new Random();
int accNo = randomGenerator.nextInt(100000);
System.out.println("Enter Initial Balance: ");
int balance = input.nextInt();
BankAccount b1 = new BankAccount(cusName, accNo, balance);
int menu;
System.out.println("Menu");
System.out.println("1. Deposit Amount");
System.out.println("2. Withdraw Amount");
System.out.println("3. Display Information");
System.out.println("4. Exit");
boolean quit = false;
do {
System.out.print("Please enter your choice: ");
menu = input.nextInt();
switch (menu) {
case 1:
System.out.print("Enter depost amount:");
Money.amount = input.nextInt();
b1.getDeposit();

break;

case 2:
System.out.println("Current Account Balance=" + b1.getBalance());
System.out.print("Enter withdrawal amount:");
Money.amount = input.nextInt();
b1.getWithdraw();

break;

case 3:
b1.display();
break;
case 4:
quit = true;
break;
}
} while (!quit);
}
<小时/>
public class Money
{

public static int accountNumber, balance=0;
static int amount;

static String name;
public void setDeposit(int amount) {

balance = balance + amount;
}
public int getDeposit()
{

balance = balance + amount;
if (amount < 0) {
System.out.println("Invalid");
}
return 1;

}
public void setBalance(int b)
{
b = balance;
}
public int getBalance()
{
return balance;
}

public void setWithdraw(int amount) {


balance = balance - amount;
}
public int getWithdraw()
{
balance = balance - amount;
if (balance < amount)
{
System.out.println("Not enough funds.");
return 1;
}
else if (amount < 0) {
System.out.println("Invalid");
return 1;}
else
return 0;
}
<小时/>
import java.util.*;

public class BankAccount extends Money {
static String name;
public static int balance, amount, acctNum;
Money customerMoney;


BankAccount(String name, int accNo, int bal) {
this.name = name;
this.acctNum = accNo;
this.balance = bal;
this.customerMoney = new Money();
}
void display() {
System.out.println("Name:" + name);
System.out.println("Account No:" + acctNum);
System.out.println("Balance:" + balance);

}

void displayBalance() {
System.out.println("Balance:" + balance);
}
public Money getMoney(){
return this.customerMoney;
}
}

最佳答案

最大的问题是语句balance=0

public static int accountNumber, balance=0;
^^^^^^^^^

每次您要插入金额时,您的余额都是

您应该使用setDeposit(input.nextInt())

public void setBalance(int b)中,b = Balance;应该是balance = b;

此外,您的 amountbalance 变量应为 Float,而不是 int 作为 balance/金额可以是23434.22

关于Java 程序无法正确添加金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13823452/

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