该程序允许用户创建多个客户端。如何允许数组等于所有这些输入?当我显示数组时,唯一显示的是添加的最后一个客户端。
static String [] clients = new String[100];
void getClient()
{
String input = new String (" ");
for(int i = 0; i < ccount; i++)
clients[i] = input;
{
input = JOptionPane.showInputDialog("Enter client ID: ");
while(checkClientID(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Only 9 digits allowed. Re-enter client ID: ");
}//end while
clientID = input;
input = JOptionPane.showInputDialog("Enter client name: ");
while(checkCname(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Only 50 alphabetical characters allowed! Re-enter client name: ");
}//end while
cname = input;
input = JOptionPane.showInputDialog("Enter client address: ");
caddress = input;
input = JOptionPane.showInputDialog("Enter client email: ");
while(checkCemail(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Must enter at least one character before @ and .com at end, Re-enter email: ");
}//end while
cemail = input;
input = JOptionPane.showInputDialog("Enter monthly service fees: ");
while(checkCfees(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Must enter real number with two decimal places, Re-enter monthly service fees: ");
}//end while
cfees = input;
input = JOptionPane.showInputDialog("Enter Employee ID assigned to client: ");
while(checkEmpID(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Only 9 digits allowed. Re-enter employee ID: ");
}//end while
cempID = input;
mainMenu();
}//end for
}//end getClient
答案 0 :(得分:0)
使用ArrayList:
首先,声明ArrayList的ArrayList:
static ArrayList<ArrayList> clients = new ArrayList<>();
每次调用函数getCliente()
时,客户都会添加到列表中:
public static void getCliente() {
String input;
clients.add(new ArrayList<>());
int i=clients.size()-1;
input = JOptionPane.showInputDialog("Enter client ID: ");
while(checkClientID(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Only 9 digits allowed. Re-enter client ID: ");
}//end while
clientID = input;
clients.get(i).add(clientID);
input = JOptionPane.showInputDialog("Enter client name: ");
while(checkCname(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Only 50 alphabetical characters allowed! Re-enter client name: ");
}//end while
cname = input;
clients.get(i).add(cname);
input = JOptionPane.showInputDialog("Enter client address: ");
caddress = input;
input = JOptionPane.showInputDialog("Enter client email: ");
while(checkCemail(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Must enter at least one character before @ and .com at end, Re-enter email: ");
}//end while
cemail = input;
clients.get(i).add(cemail);
input = JOptionPane.showInputDialog("Enter monthly service fees: ");
while(checkCfees(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Must enter real number with two decimal places, Re-enter monthly service fees: ");
}//end while
cfees = Double.parseDouble(input);
clients.get(i).add(cfees);
input = JOptionPane.showInputDialog("Enter Employee ID assigned to client: ");
while(checkEmpID(input) == false)
{
input = JOptionPane.showInputDialog("Invalid! Only 9 digits allowed. Re-enter employee ID: ");
}//end while
cempID = input;
clients.get(i).add(cempID);
//mainMenu();
}
要迭代并打印列表,我们使用此函数:
public static void printClients() {
for (ArrayList client: clients) {
System.out.println("Client ID: " + client.get(0));
System.out.println("Client name: " + client.get(1));
System.out.println("Client address: " + client.get(2));
System.out.println("Client email: " + client.get(3));
System.out.println("Monthly service fees: " + client.get(4));
System.out.println("Employee ID assigned to client: " + client.get(5));
System.out.println("--------------------------------------------------");
}
}