我一直致力于制作模拟银行交易的程序。我必须询问用户是否要存款,取款或转账。现在我正在处理账户的存款和取款选项。
当用户选择一个交易(例如存款)并输入一个数字时,我就这样做,程序会问“你想继续这个交易。显然,如果是,程序将继续进行交易,如果没有,它不会存入用户输入的号码。
我的问题是,我不知道我需要在无选项中添加什么。我不知道是否拒绝交易意味着我必须退出循环或什么但是此刻如果我没有,交易仍然会通过。下面是我输入交易但不想继续时会发生什么的视觉效果:
下面是我的整个代码。我不知道该放什么代码的部分有** 它可能对我的组织没有帮助,我很抱歉!
import java.util.Scanner;
public class BankTransactions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
double balance = 0;
double checkingBalance= 0, savingsBalance =0;
do {
double amount;
System.out.println("------------------------");
System.out.println("Select a Transaction by typing number");
System.out.println("1. Deposit");
System.out.println("2. Withdrawal");
System.out.println("3. Balance");
System.out.println("4. Exit");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if DEPOSIT is selected
//ask to deposit from checking or savings
System.out.println("------------------------");
System.out.println("Would you like to deposit in checking or savings?");
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if CHECKING is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in checking account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
// Add the amount to the checking balance
checkingBalance += amount;
System.out.println("------------------------");
System.out.println("Your checking account's balance is " + checkingBalance);
System.out.println("------------------------");
} else if (num == 2) { //if SAVINGS is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in savings account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) {
// Add the amount entered to the savings balance
savingsBalance += amount;
System.out.println("------------------------");
System.out.println("Your savings account's balance is " + savingsBalance);
System.out.println("------------------------");
**} else if (num == 2) {
//EMPTY NEEDS CODE
}**
}
} else if (num == 2) { //if withdrawal is selected
//ask to withdrawal from checking or savings
System.out.println("------------------------");
System.out.println("Would you like to withdrawal from checking or savings?");
System.out.println("1. Checking");
System.out.println("2. Savings");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if checking is selected
//enter amount to be withdrawn
System.out.println("------------------------");
System.out.println("Enter amount to withdrawal: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if you say yes to continuing
// Remove the amount from the balance
checkingBalance -= amount;
System.out.println("------------------------");
System.out.println("Your checking account's balance is " + checkingBalance);
System.out.println("------------------------");
} else if (num == 2) { //if you say no to continuing
//Do not remove amount from savings balance
//EMPTY NEEDS CODE
}
} else if (num == 2) { //if savings is selected
//enter amount to be withdrawn
System.out.println("------------------------");
System.out.println("Enter amount to withdrawal: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) { //if you say yes to continuing
// Remove the amount from the savings balance
savingsBalance -= amount;
System.out.println("------------------------");
System.out.println("Your savings account's balance is " + savingsBalance);
System.out.println("------------------------");
} else if (num == 2) { //if you say no to continuing
//Do not remove amount from savings balance
//EMPTY NEEDS CODE
}
}
} else if (num == 3) { //if balance is selected
//ask to see balance of checking or savings
System.out.println("------------------------");
System.out.println("Your Checking balance is " + checkingBalance);
System.out.println("Your Savings balance is " + savingsBalance);
System.out.println("------------------------");
num = scan.nextInt();
//needs to return to transaction options
}
} while (num != 4);
System.out.println("------------------------");
System.out.println("Good Bye!");
System.out.println("------------------------");
}
}
我被困住了,我想弄清楚。请不要发布整个代码更正。我想自己修理并学习!
答案 0 :(得分:3)
我看到的最大问题是你如何编程选项。编程的艺术通常是使问题尽可能简单并且容易扩展该问题。
(就像你制作添加数字的程序一样,你不在乎,如果你要添加3个数字或3个数字的数字)。
如果你要扩大选择,那将是痛苦的屁股:)。无论你的“颓势树”是否太大,都不可能知道那里发生了什么。
您可以创建Option类:
公共类选项{
private List<Option> options = new ArrayList<>();
private String text;
public Option(String text) {
this.text = text;
}
public void addOption(Option option) {
getOptions().add(option);
}
/**
* @return the options
*/
public List<Option> getOptions() {
return options;
}
/**
* @return the text
*/
public String getText() {
return text;
}
public String tellUsWhatWeCanDo() {
String ret = "------------------------\n";
int count = 0;
for (Option option : options) {
count++;
ret += count + ". " + option.getText() + "\n";
}
ret += "------------------------\n";
return ret;
}
public Option whereToGo() {
while (1<2) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
if ((num >= 0) && (num < options.size())){
return options.get(num);
} else {
System.out.println("wrong number");
}
}
}
}
然后你就可以这样使用:
public static void main(String[] args) throws IOException {
Option option1 = new Option("Start");
Option option2 = new Option("Deposit");
Option option3 = new Option("Withdrawal");
Option option4 = new Option("Balance");
Option option5 = new Option("Exit");
option1.addOption(option2);
option1.addOption(option3);
option1.addOption(option4);
option1.addOption(option5);
Option actualOption = option1;
while (1 < 2) {
System.out.println(actualOption.tellUsWhatWeCanDo());
actualOption = actualOption.whereToGo();
}
}
输出结果为:
------------------------
1. Deposit
2. Withdrawal
3. Balance
4. Exit
------------------------
请注意,您可以创建自动扫描程序选项,它可以以与打印行(/自动)相同的方式遍历选项。 (编辑:我补充说迭代)
你也可以使用“do”方法创建界面,并在你运行“do”方法的每个选项中实现它,这可以做更复杂的工作,如“存款,取款”等。
答案 1 :(得分:3)
交易仍然通过检查,而不是为了节省。
原因如下:
if (num == 1) { //if CHECKING is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in checking account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
// Add the amount to the checking balance
checkingBalance += amount;
System.out.println("------------------------");
System.out.println("Your checking account's balance is " + checkingBalance);
System.out.println("------------------------");
} else if (num == 2) { //if SAVINGS is selected
//enter amount to be deposited
System.out.println("------------------------");
System.out.println("Enter amount to deposit in savings account: ");
System.out.println("------------------------");
amount = scan.nextDouble();
//ask if they want to continue with transaction
System.out.println("------------------------");
System.out.println("Would you like to continue this transaction?");
System.out.println("1. Yes");
System.out.println("2. No");
System.out.println("------------------------");
num = scan.nextInt();
if (num == 1) {
// Add the amount entered to the savings balance
savingsBalance += amount;
System.out.println("------------------------");
System.out.println("Your savings account's balance is " + savingsBalance);
System.out.println("------------------------");
**} else if (num == 2) {
//EMPTY NEEDS CODE
}**
}
注意“num = scan.nextInt();”之后的区别在这两种情况下?在第一个中你指示它继续并且无论输入如何都添加,在第二种情况下,你有一个if / else语句只会在用户输入1时添加它,如果他输入2,你会什么都不做。
关于你在1或2以外的任何其他选项的情况下该做什么的问题。我会使用if语句来检查num是否为1而不使用else语句,所以如果输入任何其他选项,它将会去再次开始。 (但如果你坚持2为“否”,你可以使用else if(num!= 2){System.out.println(“Invalid Option。Go to the beginning”;}
答案 2 :(得分:0)
你使用do while循环。你需要一些终止条件。尝试使用此代码。
在你的程序中输入num = 4。它会解决你的问题。
else if (num == 2) { //if you say no to continuing
//Do not remove amount from savings balance
//EMPTY NEEDS CODE
num=4;
}