我有2节课.该程序几乎模拟了ATM.用户将要求存款,取款或查看用户银行账户的余额.
首先将余额设置为零,然后用户可以存入或取出资金.
调用的第一个类CheckingAccount
将具有方法而没有main方法,而TestCheckingAccount
具有main方法.所有输入和输出都将在TestCheckingAccount
类中进行,同时CheckingAccount
进行计算.
如果用户选择存款,则帐户的余额将被添加到用户想要存入他/她的帐户的金额中.类中的getBalance
方法CheckingAccount
返回用户帐户的余额,同时processDeposit
将用户想要的金额添加到余额中,同时processCheck
减少用户选择余额的金额.
如果用户选择查看余额,则会打印用户余额.
CheckingAccount类public class CheckingAccount{ double myBalance = 0; public double getBalance(){ return myBalance; } public void processDeposit(double amount){ myBalance += amount; } public void processCheck(double amount){ myBalance -= amount; } }TestCheckingAccount类
import java.util.Scanner; public class TestCheckingAccount { public static void main(String[] args) { CheckingAccount check = new CheckingAccount(); Scanner in = new Scanner(System.in); String choice; double moneyAdded; double moneySubtracted; do { System.out.println("Deposit"); System.out.println("Withdraw"); System.out.println("Balance"); System.out.println("Exit"); System.out.println("**********************************************"); System.out.println("Which operation would you like to use?"); choice = in.nextLine(); if(choice.equalsIgnoreCase("Deposit")) { System.out.println("How much money would you like to deposit?"); moneyAdded = in.nextDouble(); check.processDeposit(moneyAdded); } if(choice.equalsIgnoreCase("Withdraw")) { System.out.println("How much money would you like to withdraw?"); moneySubtracted = in.nextDouble(); check.processCheck(moneySubtracted); } else if(choice.equalsIgnoreCase("Balance")) { System.out.printf("$%.2f%n" , check.myBalance); } else if(choice.equalsIgnoreCase("Exit")) { System.exit(0); } } while(choice.equalsIgnoreCase("Deposit")||choice.equalsIgnoreCase("Withdraw")||choice.equalsIgnoreCase("Balance")); in.close(); }
}
程序运行正常,但我遇到的问题是程序必须询问用户他/她是否要存款,取款或查看余额,直到用户完成并退出程序.
因此,当程序询问用户是否要存入,取出或查看帐户中的余额时,该部分必须处于循环中.我把它放在一个do-while循环中.因此,当我运行程序并要求余额打印出余额然后循环好然后程序再次询问他/她是否想要提款,存款,查看余额或退出.
但当我要求程序撤销或存款时,它会运行但不会循环,而是退出循环.
我如何制作程序以便它可以取款或存款但仍然循环并返回菜单,询问用户是否要存款或取款而不仅仅是结束循环?
您可以使用无限循环并break
在"退出"条件下.此外,您可以使用try-with-resources
close语句关闭您的Scanner
.就像是,
try (Scanner in = new Scanner(System.in)) { String choice; double moneyAdded; double moneySubtracted; while (true) { System.out.println("Deposit"); System.out.println("Withdraw"); System.out.println("Balance"); System.out.println("Exit"); System.out.println("**********************************************"); System.out.println("Which operation would you like to use?"); choice = in.nextLine(); if (choice.equalsIgnoreCase("Deposit")) { System.out.println("How much money would you like to deposit?"); moneyAdded = in.nextDouble(); check.processDeposit(moneyAdded); } else if (choice.equalsIgnoreCase("Withdraw")) { System.out.println("How much money would you like to withdraw?"); moneySubtracted = in.nextDouble(); check.processCheck(moneySubtracted); } else if (choice.equalsIgnoreCase("Balance")) { System.out.printf("$%.2f%n", check.myBalance); } else if (choice.equalsIgnoreCase("Exit")) { break; } } }