取款和存款后如何更新余额(取款,存款,余额查询)java
how can i update balance after doing withdrawal and deposits(withdraw,deposit,balance inquiry) java
我在 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()
{
}
}
--------------------------------主要------------ ----------------------
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);
}
}
为什么要实例化 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()
,这样您就可以将 金额 创建为双倍金额。
如果您没有看到余额查询,那是因为显然您的 accBalance() 方法是空白的。像这样编辑它:
public void accBalance(){
System.out.println("Your balance is: "+this.balance);
}
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);
}
}
试试这个代码,与你的比较,找出问题所在,如果你需要更多帮助,也可以问我
我在 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()
{
}
}
--------------------------------主要------------ ----------------------
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);
}
}
为什么要实例化 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()
,这样您就可以将 金额 创建为双倍金额。
如果您没有看到余额查询,那是因为显然您的 accBalance() 方法是空白的。像这样编辑它:
public void accBalance(){
System.out.println("Your balance is: "+this.balance);
}
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);
}
}
试试这个代码,与你的比较,找出问题所在,如果你需要更多帮助,也可以问我