【发布时间】:2015-04-06 11:21:38
【问题描述】:
我的程序要求用户创建一个银行帐户。目前它只能创建一个帐户并打印出来。我想扩展我的程序,以便它可以通过一次又一次地选择菜单选项来创建多个帐户,最后打印所有帐户详细信息。
package bankapp_assignment;
import java.util.Scanner;
public class BankApp_Assignment {
public static void main(String[] args) {
BankAccount[] accounts = new BankAccount[10];
Scanner sc = new Scanner(System.in);
int userChoose;
String name = null;
int accNum = 0;
double initiateAmount = 0;
double newAmm = 0;
double depOrWith = 0;
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "...................\n"
+ "...................\n\n"
+ "Choose your optiin:\n"
+ "1. Create new account\n"
+ "2. View all the accounts property\n"
+ "3. Quit\n\n");
System.out.println("*************\n"
+ "************");
while (true) {
userChoose = sc.nextInt();
sc.nextLine();
if (userChoose == 1) {
/*the user must be able to create multiple accounts, let's say 10 accounts for instance
To open another new account the user should choose the menu option "1" again and continue...
*/
System.out.println("Enter your full name:");
name = sc.nextLine();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
System.out.println("Enter an initiating amount:");
initiateAmount = sc.nextDouble();
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 2) {//view all the accounts property (including account number and initial balance)
} else if (userChoose == 3) {
System.exit(0);
}
}
}
}
银行账户:
package bankapp_assignment;
public class BankAccount {
public void createAcc(){
}
}
在纳曼回答后编辑:
public static void main(String[] args) {
BankAccount[] accounts = new BankAccount[10];
Scanner sc = new Scanner(System.in);
int userChoose;
String name = null;
int accNum = 0;
double initiateAmount = 0;
double newAmm = 0;
double depOrWith = 0;
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "...................\n"
+ "...................\n\n"
+ "Choose your optiin:\n"
+ "1. Create new account\n"
+ "2. View all the accounts property\n"
+ "3. Quit\n\n");
System.out.println("*************\n"
+ "************");
while (true) {
userChoose = sc.nextInt();
sc.nextLine();
if (userChoose == 1) {
/*the user must be able to create multiple accounts, let's say 10 accounts for instance
To open another new account the user should choose the menu option "1" again and continue...
*/
System.out.println("Enter your full name:");
name = sc.nextLine();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
System.out.println("Enter an initiating amount:");
initiateAmount = sc.nextDouble();
System.out.println("\n-----------\n"
+ "------------");
accounts[numOfAcc]=bankAcc;
numOfAcc++;
} else if (userChoose == 2) {//view all the accounts property (including account number and initial balance)
for(BankAccount bankAccount: accounts){
System.out.println("Your name: " + name);
System.out.println("Your account number: " + accNum);
System.out.println("Your current balance: " + initiateAmount);
System.out.println("\n-----------\n"
+ "------------");
}
} else if (userChoose == 3) {
System.exit(0);
}
}
}
}
【问题讨论】:
标签: java arrays object if-statement java.util.scanner