import java.util.Scanner; 公共班级ATM {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int Account01 = 3000;
int Account02 = 2000;
int Account03 = 1000;
String username;
int pin, transaction, counter=1, menu, chooseAccount;
int withdraw_amount, deposited_amount, transfer_amount, transfer_account = 0;
do{
System.out.print("Enter username: ");
username = console.next();
System.out.print("Enter PIN: ");
pin = console.nextInt();
if ((username.equalsIgnoreCase("Account01")) && (pin == 1234 ))
{
System.out.println("PIN Accepted");
System.out.println("\n\nSelect a menu:");
System.out.println("[1] Balance Inquiry\n[2] Withdrawal\n[3] Deposit\n[4] Transfer Amount");
System.out.print("\nEnter your choice: ");
menu = console.nextInt();
do{
if (menu == 1)
{
System.out.print("\n\nBALANCE INQUIRY");
System.out.print("\nYour Current Balance is: " + Account01);
}
else if (menu ==2)
{
do{
System.out.print("\n\nWITHDRAWAL");
System.out.print("\nEnter amount: ");
withdraw_amount = console.nextInt();
if(((withdraw_amount % 100) == 0) && ( (withdraw_amount >= 200) &&
(withdraw_amount <= Account01) && (withdraw_amount <= 10000) ))
{
Account01 = Account01 - withdraw_amount;
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect amount!\n");
else{
System.out.println("Card has been captured!");
break;
}
counter++;
}while(counter<=3);
}
else if (menu == 3)
{
do
{
System.out.print("\n\nDEPOSIT");
System.out.print("\nEnter amount: ");
deposited_amount = console.nextInt();
if( ((deposited_amount % 100) == 0) && (deposited_amount <= 10000) )
{
Account01 = Account01 + deposited_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else if (menu == 4)
{
do{
System.out.print("\n\nTRANSFER PAYMENT");
System.out.print("\nEnter amount: ");
transfer_amount = console.nextInt();
System.out.print("[1] account01\n[2] account02\n[3] account03\n");
System.out.print("Chose account: ");
chooseAccount = console.nextInt();
if(chooseAccount == 1)
{
transfer_account = Account01;
}
else if(chooseAccount == 2)
{
transfer_account = Account02;
}
else if(chooseAccount == 3)
{
transfer_account = Account03;
}
else System.out.println("invalid number entered.");
if( ((transfer_amount % 100) == 0) && (transfer_amount <= 10000) )
{
Account01 = Account01 - transfer_amount;
transfer_account = transfer_account + transfer_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
System.out.println("\n\n\nDo you want another transaction?\nType [1] if YES. Type [2] if NO");
System.out.print("\nEnter your choice: ");
transaction = console.nextInt();
if (transaction == 1)
{
System.out.println("\n\nSelect a menu:");
System.out.println("[1] Balance Inquiry\n[2] Withdrawal\n[3] Deposit\n[4] Transfer Amount");
System.out.print("\nEnter your choice: ");
menu = console.nextInt();
do{
if (menu == 1)
{
System.out.print("\n\nBALANCE INQUIRY");
System.out.print("\nYour Current Balance is: " + Account01);
}
else if (menu ==2)
{
do{
System.out.print("\n\nWITHDRAWAL");
System.out.print("\nEnter amount: ");
withdraw_amount = console.nextInt();
if(((withdraw_amount % 100) == 0) && ( (withdraw_amount >= 200) &&
(withdraw_amount <= Account01) && (withdraw_amount <= 10000) ))
{
Account01 = Account01 - withdraw_amount;
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect amount!\n");
else{
System.out.println("Card has been captured!");
break;
}
counter++;
}while(counter<=3);
}
else if (menu == 3)
{
do
{
System.out.print("\n\nDEPOSIT");
System.out.print("\nEnter amount: ");
deposited_amount = console.nextInt();
if( ((deposited_amount % 100) == 0) && (deposited_amount <= 10000) )
{
Account01 = Account01 + deposited_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else if (menu == 4)
{
do{
System.out.print("\n\nTRANSFER PAYMENT");
System.out.print("\nEnter amount: ");
transfer_amount = console.nextInt();
System.out.print("[1] account01\n[2] account02\n[3] account03\n");
System.out.print("Chose account: ");
chooseAccount = console.nextInt();
if(chooseAccount == 1)
{
transfer_account = Account01;
}
else if(chooseAccount == 2)
{
transfer_account = Account02;
}
else if(chooseAccount == 3)
{
transfer_account = Account03;
}
else System.out.println("invalid number entered.");
if( ((transfer_amount % 100) == 0) && (transfer_amount <= 10000) )
{
Account01 = Account01 - transfer_amount;
transfer_account = transfer_account + transfer_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else
{
System.out.println("\nThank You!!!\nGood Bye!!!");
}
break;
}while((menu >= 1) && (menu <= 4));
break;
}
else if ((username.equalsIgnoreCase("Account02")) && (pin == 4321))
{
System.out.println("PIN Accepted");
System.out.println("\n\nSelect a menu:");
System.out.println("[1] Balance Inquiry\n[2] Withdrawal\n[3] Deposit\n[4] Transfer Amount");
System.out.print("\nEnter your choice: ");
menu = console.nextInt();
do{
if (menu == 1)
{
System.out.print("\n\nBALANCE INQUIRY");
System.out.print("\nYour Current Balance is: " + Account02);
}
else if (menu ==2)
{
do{
System.out.print("\n\nWITHDRAWAL");
System.out.print("\nEnter amount: ");
withdraw_amount = console.nextInt();
if(((withdraw_amount % 100) == 0) && ( (withdraw_amount >= 200) &&
(withdraw_amount <= Account02) && (withdraw_amount <= 10000) ))
{
Account02 = Account02 - withdraw_amount;
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect amount!\n");
else{
System.out.println("Card has been captured!");
break;
}
counter++;
}while(counter<=3);
}
else if (menu == 3)
{
do
{
System.out.print("\n\nDEPOSIT");
System.out.print("\nEnter amount: ");
deposited_amount = console.nextInt();
if( ((deposited_amount % 100) == 0) && (deposited_amount <= 10000) )
{
Account02 = Account02 + deposited_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else if (menu == 4)
{
do{
System.out.print("\n\nTRANSFER PAYMENT");
System.out.print("\nEnter amount: ");
transfer_amount = console.nextInt();
System.out.print("[1] account01\n[2] account02\n[3] account03\n");
System.out.print("Chose account: ");
chooseAccount = console.nextInt();
if(chooseAccount == 1)
{
transfer_account = Account01;
}
else if(chooseAccount == 2)
{
transfer_account = Account02;
}
else if(chooseAccount == 3)
{
transfer_account = Account03;
}
else System.out.println("invalid number entered.");
if( ((transfer_amount % 100) == 0) && (transfer_amount <= 10000) )
{
Account02 = Account02 - transfer_amount;
transfer_account = transfer_account + transfer_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
System.out.println("\n\n\nDo you want another transaction?\nType [1] if YES. Type [2] if NO");
System.out.print("\nEnter your choice: ");
transaction = console.nextInt();
if (transaction == 1)
{
System.out.println("\n\nSelect a menu:");
System.out.println("[1] Balance Inquiry\n[2] Withdrawal\n[3] Deposit\n[4] Transfer Amount");
System.out.print("\nEnter your choice: ");
menu = console.nextInt();
do{
if (menu == 1)
{
System.out.print("\n\nBALANCE INQUIRY");
System.out.print("\nYour Current Balance is: " + Account02);
}
else if (menu ==2)
{
do{
System.out.print("\n\nWITHDRAWAL");
System.out.print("\nEnter amount: ");
withdraw_amount = console.nextInt();
if(((withdraw_amount % 100) == 0) && ( (withdraw_amount >= 200) &&
(withdraw_amount <= Account02) && (withdraw_amount <= 10000) ))
{
Account02 = Account02 - withdraw_amount;
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect amount!\n");
else{
System.out.println("Card has been captured!");
break;
}
counter++;
}while(counter<=3);
}
else if (menu == 3)
{
do
{
System.out.print("\n\nDEPOSIT");
System.out.print("\nEnter amount: ");
deposited_amount = console.nextInt();
if( ((deposited_amount % 100) == 0) && (deposited_amount <= 10000) )
{
Account02 = Account02 + deposited_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else if (menu == 4)
{
do{
System.out.print("\n\nTRANSFER PAYMENT");
System.out.print("\nEnter amount: ");
transfer_amount = console.nextInt();
System.out.print("[1] account01\n[2] account02\n[3] account03\n");
System.out.print("Chose account: ");
chooseAccount = console.nextInt();
if(chooseAccount == 1)
{
transfer_account = Account01;
}
else if(chooseAccount == 2)
{
transfer_account = Account02;
}
else if(chooseAccount == 3)
{
transfer_account = Account03;
}
else System.out.println("invalid number entered.");
if( ((transfer_amount % 100) == 0) && (transfer_amount <= 10000) )
{
Account02 = Account02 - transfer_amount;
transfer_account = transfer_account + transfer_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else
{
System.out.println("\nThank You!!!\nGood Bye!!!");
}
break;
}while((menu >= 1) && (menu <= 4));
break;
}
else if ((username.equalsIgnoreCase("Account03")) && (pin == 5555))
{
System.out.println("PIN Accepted");
System.out.println("\n\nSelect a menu:");
System.out.println("[1] Balance Inquiry\n[2] Withdrawal\n[3] Deposit\n[4] Transfer Amount");
System.out.print("\nEnter your choice: ");
menu = console.nextInt();
do{
if (menu == 1)
{
System.out.print("\n\nBALANCE INQUIRY");
System.out.print("\nYour Current Balance is: " + Account03);
}
else if (menu ==2)
{
do{
System.out.print("\n\nWITHDRAWAL");
System.out.print("\nEnter amount: ");
withdraw_amount = console.nextInt();
if(((withdraw_amount % 100) == 0) && ( (withdraw_amount >= 200) &&
(withdraw_amount <= Account03) && (withdraw_amount <= 10000) ))
{
Account03 = Account03 - withdraw_amount;
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect amount!\n");
else{
System.out.println("Card has been captured!");
break;
}
counter++;
}while(counter<=3);
}
else if (menu == 3)
{
do
{
System.out.print("\n\nDEPOSIT");
System.out.print("\nEnter amount: ");
deposited_amount = console.nextInt();
if( ((deposited_amount % 100) == 0) && (deposited_amount <= 10000) )
{
Account03 = Account03 + deposited_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else if (menu == 4)
{
do{
System.out.print("\n\nTRANSFER PAYMENT");
System.out.print("\nEnter amount: ");
transfer_amount = console.nextInt();
System.out.print("[1] account01\n[2] account02\n[3] account03\n");
System.out.print("Chose account: ");
chooseAccount = console.nextInt();
if(chooseAccount == 1)
{
transfer_account = Account01;
}
else if(chooseAccount == 2)
{
transfer_account = Account02;
}
else if(chooseAccount == 3)
{
transfer_account = Account03;
}
else System.out.println("invalid number entered.");
if( ((transfer_amount % 100) == 0) && (transfer_amount <= 10000) )
{
Account03 = Account03 - transfer_amount;
transfer_account = transfer_account + transfer_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
System.out.println("\n\n\nDo you want another transaction?\nType [1] if YES. Type [2] if NO");
System.out.print("\nEnter your choice: ");
transaction = console.nextInt();
if (transaction == 1)
{
System.out.println("\n\nSelect a menu:");
System.out.println("[1] Balance Inquiry\n[2] Withdrawal\n[3] Deposit\n[4] Transfer Amount");
System.out.print("\nEnter your choice: ");
menu = console.nextInt();
do{
if (menu == 1)
{
System.out.print("\n\nBALANCE INQUIRY");
System.out.print("\nYour Current Balance is: " + Account03);
}
else if (menu ==2)
{
do{
System.out.print("\n\nWITHDRAWAL");
System.out.print("\nEnter amount: ");
withdraw_amount = console.nextInt();
if(((withdraw_amount % 100) == 0) && ( (withdraw_amount >= 200) &&
(withdraw_amount <= Account03) && (withdraw_amount <= 10000) ))
{
Account03 = Account03 - withdraw_amount;
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect amount!\n");
else{
System.out.println("Card has been captured!");
break;
}
counter++;
}while(counter<=3);
}
else if (menu == 3)
{
do
{
System.out.print("\n\nDEPOSIT");
System.out.print("\nEnter amount: ");
deposited_amount = console.nextInt();
if( ((deposited_amount % 100) == 0) && (deposited_amount <= 10000) )
{
Account03 = Account03 + deposited_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else if (menu == 4)
{
do{
System.out.print("\n\nTRANSFER PAYMENT");
System.out.print("\nEnter amount: ");
transfer_amount = console.nextInt();
System.out.print("[1] account01\n[2] account02\n[3] account03\n");
System.out.print("Chose account: ");
chooseAccount = console.nextInt();
if(chooseAccount == 1)
{
transfer_account = Account01;
}
else if(chooseAccount == 2)
{
transfer_account = Account02;
}
else if(chooseAccount == 3)
{
transfer_account = Account03;
}
else System.out.println("invalid number entered.");
if( ((transfer_amount % 100) == 0) && (transfer_amount <= 10000) )
{
Account03 = Account03 - transfer_amount;
transfer_account = transfer_account + transfer_amount;
break;
}
// error catching
if(counter < 3)
System.out.println("Error: Incorrect amount.\n");
else
{
System.out.println("Card has been captured.");
break;
}
counter++;
}while(counter <= 3);
}
else
{
System.out.println("\nThank You!!!\nGood Bye!!!");
}
break;
}while((menu >= 1) && (menu <= 4));
break;
}
else if ((username.equalsIgnoreCase("x")) && (pin == 0000))
{
System.out.println("Program will be terminated!");
break;
}
// error catching
if(counter<3)
System.out.println("Error: Incorrect username or password.\n");
else{
System.out.println("Card has been captured!");
System.out.println("\n\nDo you want to continue?\nType [1] if YES. Type [2] if NO ");
System.out.print("Enter your choice: ");
transaction = console.nextInt();
if (transaction == 1)
{
System.out.println("\n\nInsert your card.");
System.out.print("\nEnter username: ");
username = console.next();
System.out.print("Enter PIN: ");
pin = console.nextInt();
}
else
{
System.out.println("\nThank You!!!\nGood Bye!!!");
}
break;
}
counter++;
}while(counter<=3);
}
}
我正在制作一个atm机器程序。我的计划有什么问题,为什么它不起作用?有什么建议吗?我真的需要解决这个问题,我需要它汤姆! :(
答案 0 :(得分:2)
您的代码将无法编译,因为您使用了具有常规形式的do {} while循环:
do {
statement(s)
} while (expression);
并且您没有在其中三个中添加表达式部分(从代码的开头)。 你写没有表达式的语句。
我不知道你的代码,但它足以添加:
}while(true);
}
}while(true);
}
}while(true);
}
}
编译它。
但是你应该认真考虑在多个类和方法之间划分你的代码,因为它很难阅读和维护(你可能知道)。