我一直在制作一个模拟银行交易的程序。我必须询问用户是否要存款、取款或转账。现在我正在研究账户的存款和取款选项。
当用户选择一笔交易(例如存款)并输入一个数字时,我做了,所以程序会询问“你想继续这笔交易吗?显然,如果是,程序将继续交易,如果不是,它不会存入用户输入的号码。
我的问题是,我不知道我需要在 no 选项中添加什么。我不知道拒绝交易是否意味着我必须退出循环或什么,但目前如果我点击“否”,交易仍然会通过。下面是当我输入交易但不想继续时发生的情况的视觉效果:
下面是我的整个代码。我不知道该放什么的代码部分有**它可能对我的组织没有帮助我很抱歉!
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("------------------------");
}
}
我被困住了,我想弄清楚。请不要发布更正的整个代码。我想自己修复它并学习!