如何将多个值(具有不同类型)存储在数组中并打印出来?
How do I store multiple values (with different types) in an array and print them out?
我的程序要求用户创建一个银行帐户。目前它只能创建一个帐户并打印出来。我想扩展我的程序,以便它可以通过一次又一次地选择菜单选项创建多个帐户,最后打印所有帐户详细信息。
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(){
}
}
在 Naman 回答后进行编辑:
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);
}
}
}
}
1) 创建对象class:例如
class BankAccount{
private String userName;
private String userSurname;
private String birthDay;
private int accountNumber;
private int ibanNo;
private int balance;
public BankAccount(String userName, String userSurname, String birthDay, int accountNumber, int ibanNo, int balance) {
super();
this.userName = userName;
this.userSurname = userSurname;
this.birthDay = birthDay;
this.accountNumber = accountNumber;
this.ibanNo = ibanNo;
this.balance = balance;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSurname() {
return userSurname;
}
public void setUserSurname(String userSurname) {
this.userSurname = userSurname;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public int getIbanNo() {
return ibanNo;
}
public void setIbanNo(int ibanNo) {
this.ibanNo = ibanNo;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
2) 在您的 main class;
中使用此对象创建一个全局列表
List<BankAccount> accounts = new ArrayList<BankAccount>();
3) 现在您可以将新帐户添加到列表中;
accounts.add(new BankAccount(String userName, String userSurname, String birthDay, int accountNumber, int ibanNo, int balance));
4) 现在您可以使用帐户列表添加/获取/删除帐户。
创建银行帐户详细信息的对象并简单地添加到 ArrayList
使用 java.util 包中的 ArrayList 数据类型
创建一个包含银行帐户详细信息的 class,只需将其导入此 class 并编写以下代码
List <BankAccount> accounts = new <BankAccount> ArrayList();
BankAccount object = new BankAccount();
//Set field here like
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();
简单使用后
accounts.add(object);
在这种情况下存储通用数据类型或对象是完美的,
还可以在 if -then- else 块上使用 switch case 来阻止它的可读性和便利性
还可以使用增强的 for 循环查看帐户
for(BankAccount account:accounts){
System.out.println("ALL DETAILS FROM OBJECT");
}
如果你想继续数组那么你可以先初始化静态变量:
static int number = 0;
如下更改您的区块:
if (userChoose == 1) {
BankAccount ac = new BankAccount();
//set account properties;
accounts[number] = ac;
number++;
}
您可以像下面这样检索数组:
for(BankAccount bankAccount: accounts){
//bankAccount.getProperty();
}
你的代码应该像
BankAccount bankAcc = null;
if (userChoose == 1) {
bankAcc = new BankAccount();
/*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();
bankAcc.setName(name);//setter method of your bankAccount bean
System.out.println("Choose an account number:");
accNum = sc.nextInt();
bankAcc.setAccountNum(accNum);
System.out.println("Enter an initiating amount:");
initiateAmount = sc.nextDouble();
bankAcc.setInitialAmount(initialAmount);
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: " + bankAccount.getName());
System.out.println("Your account number: " + bankAccount.getAccountNum());
System.out.println("Your current balance: " + bankAccount.getInitialAmount());
System.out.println("\n-----------\n"
+ "------------");
}
带有有效答案的完整代码
package tester;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BankApp {
static class BankAccount {
private String name;
private int accNum;
private double initiateAmount;
public BankAccount() {
this.name = null;
this.accNum = 0;
this.initiateAmount = 0;
}
public void setAccNum(int accNum) {
this.accNum = accNum;
}
public void setInitiateAmount(double initiateAmount) {
this.initiateAmount = initiateAmount;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name + "\t" + accNum + "\t" + initiateAmount;
}
}
public static void main(String[] args) {
List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "Choose your option:\n"
+ "1. Create new account\n"
+ "2. View all the accounts property\n"
+ "3. Quit\n");
int option = sc.nextInt();
sc.nextLine();
switch(option){
case 1:
BankAccount account = new BankAccount();
System.out.println("Enter your full name:");
account.setName(sc.nextLine());
System.out.println("Choose an account number:");
account.setAccNum(sc.nextInt());
System.out.println("Enter an initiating amount:");
account.setInitiateAmount(sc.nextDouble());
bankAccounts.add(account);
break;
case 2:
System.out.println("Name\tAcc No\tAmount");
for (BankAccount bankAccount : bankAccounts) {
System.out.println(bankAccount);
}
System.out.println("\n\n");
break;
case 3:
return;
}
}
}
}
如果您想要 BankAccount
在单独的文件中,请执行以下操作。
文件BankAccount.java
public class BankAccount {
...
...
}
我的程序要求用户创建一个银行帐户。目前它只能创建一个帐户并打印出来。我想扩展我的程序,以便它可以通过一次又一次地选择菜单选项创建多个帐户,最后打印所有帐户详细信息。
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(){
}
}
在 Naman 回答后进行编辑:
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);
}
}
}
}
1) 创建对象class:例如
class BankAccount{
private String userName;
private String userSurname;
private String birthDay;
private int accountNumber;
private int ibanNo;
private int balance;
public BankAccount(String userName, String userSurname, String birthDay, int accountNumber, int ibanNo, int balance) {
super();
this.userName = userName;
this.userSurname = userSurname;
this.birthDay = birthDay;
this.accountNumber = accountNumber;
this.ibanNo = ibanNo;
this.balance = balance;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSurname() {
return userSurname;
}
public void setUserSurname(String userSurname) {
this.userSurname = userSurname;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public int getIbanNo() {
return ibanNo;
}
public void setIbanNo(int ibanNo) {
this.ibanNo = ibanNo;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
2) 在您的 main class;
中使用此对象创建一个全局列表List<BankAccount> accounts = new ArrayList<BankAccount>();
3) 现在您可以将新帐户添加到列表中;
accounts.add(new BankAccount(String userName, String userSurname, String birthDay, int accountNumber, int ibanNo, int balance));
4) 现在您可以使用帐户列表添加/获取/删除帐户。
创建银行帐户详细信息的对象并简单地添加到 ArrayList 使用 java.util 包中的 ArrayList 数据类型 创建一个包含银行帐户详细信息的 class,只需将其导入此 class 并编写以下代码
List <BankAccount> accounts = new <BankAccount> ArrayList();
BankAccount object = new BankAccount();
//Set field here like
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();
简单使用后
accounts.add(object);
在这种情况下存储通用数据类型或对象是完美的, 还可以在 if -then- else 块上使用 switch case 来阻止它的可读性和便利性
还可以使用增强的 for 循环查看帐户
for(BankAccount account:accounts){
System.out.println("ALL DETAILS FROM OBJECT");
}
如果你想继续数组那么你可以先初始化静态变量:
static int number = 0;
如下更改您的区块:
if (userChoose == 1) {
BankAccount ac = new BankAccount();
//set account properties;
accounts[number] = ac;
number++;
}
您可以像下面这样检索数组:
for(BankAccount bankAccount: accounts){
//bankAccount.getProperty();
}
你的代码应该像
BankAccount bankAcc = null;
if (userChoose == 1) {
bankAcc = new BankAccount();
/*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();
bankAcc.setName(name);//setter method of your bankAccount bean
System.out.println("Choose an account number:");
accNum = sc.nextInt();
bankAcc.setAccountNum(accNum);
System.out.println("Enter an initiating amount:");
initiateAmount = sc.nextDouble();
bankAcc.setInitialAmount(initialAmount);
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: " + bankAccount.getName());
System.out.println("Your account number: " + bankAccount.getAccountNum());
System.out.println("Your current balance: " + bankAccount.getInitialAmount());
System.out.println("\n-----------\n"
+ "------------");
}
带有有效答案的完整代码
package tester;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BankApp {
static class BankAccount {
private String name;
private int accNum;
private double initiateAmount;
public BankAccount() {
this.name = null;
this.accNum = 0;
this.initiateAmount = 0;
}
public void setAccNum(int accNum) {
this.accNum = accNum;
}
public void setInitiateAmount(double initiateAmount) {
this.initiateAmount = initiateAmount;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name + "\t" + accNum + "\t" + initiateAmount;
}
}
public static void main(String[] args) {
List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "Choose your option:\n"
+ "1. Create new account\n"
+ "2. View all the accounts property\n"
+ "3. Quit\n");
int option = sc.nextInt();
sc.nextLine();
switch(option){
case 1:
BankAccount account = new BankAccount();
System.out.println("Enter your full name:");
account.setName(sc.nextLine());
System.out.println("Choose an account number:");
account.setAccNum(sc.nextInt());
System.out.println("Enter an initiating amount:");
account.setInitiateAmount(sc.nextDouble());
bankAccounts.add(account);
break;
case 2:
System.out.println("Name\tAcc No\tAmount");
for (BankAccount bankAccount : bankAccounts) {
System.out.println(bankAccount);
}
System.out.println("\n\n");
break;
case 3:
return;
}
}
}
}
如果您想要 BankAccount
在单独的文件中,请执行以下操作。
文件BankAccount.java
public class BankAccount {
...
...
}