我用Java做作业。我的任务是建立一家可以提取,存款和查询余额的银行。我的问题是充值和提款后我无法更新余额...我已经尽了一切努力,但仍然无法理解逻辑。有人可以帮忙添加到我的程序中吗...谢谢
import java.util.Scanner;
public class bankJava
{
Scanner input = new Scanner(System.in);
double balance;
double amount;
public void withdraw()
{
System.out.println("Enter amount: ");
amount = input.nextInt();
balance = balance - amount;
}
public void deposit()
{
System.out.println("Enter amount: ");
amount = input.nextInt();
balance = balance + amount;
}
public void accBalance()
{
}
}
--------------------------------- MAIN ------------- -------------------
import java.util.Scanner;
public class bankJavaTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int action;
bankJava wdraw = new bankJava();
bankJava dposit = new bankJava();
bankJava balanceInquiry = new bankJava();
bankJava amount = new bankJava();
do{
System.out.println("Choose Action: ");
System.out.println("(1) Withdraw");
System.out.println("(2) Deposit");
System.out.println("(3) Balance Inquiry");
System.out.println("(4) Exit");
action = input.nextInt();
switch(action){
//---------WITHDRAW------------//
case 1 :
System.out.println("******Withdraw******");
wdraw.withdraw();
System.out.println("***************************");
break;
//---------DEPOSIT------------//
case 2 :
System.out.println("******Deposit******");
dposit.deposit();
System.out.println("***************************");
break;
//-----------Balance Inquiry-------//
case 3 :
System.out.println("******Balance Inquiry******");
balanceInquiry.accBalance();
System.out.println("***************************");
break;
case 4 :
System.out.println("Thank you for choosing our bank!");
break;
default :
System.out.println("Invalid action.");
break;
}
}while(action != 4);
}
}
答案 0 :(得分:0)
为什么要对4个不同的JavaBank进行注册?对每个操作执行此操作,您将在不同的对象中执行每个方法。如果我很了解您的问题,我认为您可以轻松地在同一对象中解决问题。
import java.util.Scanner;
public class bankJavaTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int action;
bankJava myJavaBank = new bankJava(); //creating the bank
do{
System.out.println("Choose Action: ");
System.out.println("(1) Withdraw");
System.out.println("(2) Deposit");
System.out.println("(3) Balance Inquiry");
System.out.println("(4) Exit");
action = input.nextInt();
switch(action){
//---------WITHDRAW------------//
case 1 :
System.out.println("******Withdraw******");
myJavaBank.withdraw(); //withdrawing from it
System.out.println("***************************");
break;
//---------DEPOSIT------------//
case 2 :
System.out.println("******Deposit******");
myJavaBank.deposit(); //deposit from it
System.out.println("***************************");
break;
//-----------Balance Inquiry-------//
case 3 :
System.out.println("******Balance Inquiry******");
myJavaBank.accBalance();
//You don't post this method but I suppose it will refer to the same bank
System.out.println("***************************");
break;
case 4 :
System.out.println("Thank you for choosing our bank!");
break;
default :
System.out.println("Invalid action.");
break;
}
}while(action != 4);
}
}
现在应该可以工作了。使用您的代码,您有4个不同的银行,一个仅用于存款,一个仅用于提款,依此类推。因此,一家银行将继续增加资金,而另一家银行将继续减少负数。
将 amount 参数设置为JavaBank参数,应在每个方法中添加局部变量,以便它不定义Bank。
类似
public class bankJava
{
Scanner input = new Scanner(System.in);
double balance;
public void withdraw()
{
System.out.println("Enter amount: ");
double amount = input.nextInt();
balance = balance - amount;
}
public void deposit()
{
System.out.println("Enter amount: ");
double amount = input.nextInt();
balance = balance + amount;
}
我还建议将input.nextInt()
更改为input.nextDouble()
,以便将 amount 创建为两倍。
如果您没有看到“余额查询”是因为显然您的accBalance()方法为空。像这样编辑它:
public void accBalance(){
System.out.println("Your balance is: "+this.balance);
}
答案 1 :(得分:0)
import java.util.Scanner;
public class BankJava {
double balance = 0;
double amount;
public void withdraw(int amount) {
balance = balance - amount;
}
public void deposit(int amount) {
balance = balance + amount;
}
public double showBalance() {
return balance;
}
public static void main(String[] args) {
BankJava bank = new BankJava();
Scanner input = new Scanner(System.in);
int action;
int amount;
do{
System.out.println("Choose Action: ");
System.out.println("(1) Withdraw");
System.out.println("(2) Deposit");
System.out.println("(3) Balance Inquiry");
System.out.println("(4) Exit");
action = input.nextInt();
switch(action){
//---------WITHDRAW------------//
case 1 :
System.out.println("******Withdraw******");
System.out.println("enter amount:");
amount = input.nextInt();
bank.withdraw(amount);
System.out.println("***************************");
System.out.println("Your balance is now: " + bank.showBalance());
break;
//---------DEPOSIT------------//
case 2 :
System.out.println("******Deposit******");
System.out.println("enter amount:");
amount = input.nextInt();
bank.deposit(amount);
System.out.println("***************************");
System.out.println("Your balance is now: " + bank.showBalance());
break;
//-----------Balance Inquiry-------//
case 3 :
System.out.println("******Balance Inquiry******");
System.out.println("Your balance is: " + bank.showBalance());
System.out.println("***************************");
break;
case 4 :
System.out.println("Thank you for choosing our bank!");
break;
default :
System.out.println("Invalid action.");
break;
}
}while(action != 4);
}
}
尝试一下此代码,与您的代码进行比较,找出问题所在,也可以问我是否需要更多帮助