我有一个班级,用户输入有关员工的信息(姓名,地址,雇用日期),以确定用户确定的员工人数。
import java.util.ArrayList;
import java.util.Scanner;
public class Employee {
public static void main(String [] args){
String employeeName = null;
String employeeAddress = null;
String hireDate = null;
Scanner userInput = new Scanner(System.in);
System.out.println("How many employees would you like to enter information for?");
int numEmployees = userInput.nextInt();
for( int i = 0; i < numEmployees; i++) {
Scanner input = new Scanner(System.in);
System.out.println("Enter employees first name: ");
String firstName = input.nextLine();
System.out.println("Enter employees last name: ");
String lastName = input.nextLine();
System.out.println("Enter street employee lives on:");
String street = input.nextLine();
System.out.println("Enter city employee lives in:");
String city = input.nextLine();
System.out.println("Enter state employee lives in:");
String state = input.nextLine();
System.out.println("Enter employee's zip code:");
String zip = input.nextLine();
System.out.println("Enter month employee was hired:");
String month = input.nextLine();
System.out.println("Enter day employee was hired:");
String day = input.nextLine();
System.out.println("Enter year employee was hired:");
String year = input.nextLine();
Name name = new Name(firstName, lastName);
Address address = new Address(street, city, state, zip);
Date date = new Date(month, day, year);
employeeName = name.getName();
employeeAddress = address.getAddress();
hireDate = date.getDate();
ArrayList<String> obj = new ArrayList<String>();
obj.add(employeeName);
obj.add(employeeAddress);
obj.add(hireDate);
System.out.println(obj);
}
}
}
我正在尝试让程序通过用户提示,获取该信息并将其放在ArrayList中的某个位置,然后重复多次用户先前确定并在结束时一次显示所有内容。像这样:
FirstName LastName, Address, Hire Date
FirstName LastName, Address, Hire Date
等等。现在,我的代码将通过用户提示运行,显示该代码,然后运行下一轮提示并显示:
Enter Name:
Name
Enter Address:
Address
Enter Hire Date:
Hire Date
[Name, Address, Hire Date]
Enter Name:
Name
Enter Address:
Address
Enter Hire Date:
Hire Date
[Name, Address, Hire Date]
我意识到这是因为我的数组和数组显示在for循环中,但当我将数组移到循环外时,我得不到显示。当我只在循环外移动数组显示时,我的代码无法编译。
答案 0 :(得分:2)
移动此行:
System.out.println(obj);
超出for
循环。并使用List
来包含ArrayList
。然后将其显示在单独的for
循环中:
List<ArrayList<String>> objs = new ArrayList<ArrayList<String>>(); //use list of objs
for( int i = 0; i < numEmployees; i++ )
{
//all the inputs
ArrayList<String> obj = new ArrayList<String>();
obj.add(employeeName);
obj.add(employeeAddress);
obj.add(hireDate);
objs.add(obj);
}
for( int i = 0; i < numEmployees; i++ ) //for loop just to display
System.out.println(objs.get(i)); //display each element here
答案 1 :(得分:0)
您要打印的是[string,string,string]类型的元组。这可以表示为List。然后,对于每个员工,您要将此元组存储在另一个类型为List<List<String>>
的列表中。
public static void main( String [] args )
{
String employeeName = null;
String employeeAddress = null;
String hireDate = null;
Scanner userInput = new Scanner(System.in);
System.out.println("How many employees would you like to enter information for?");
int numEmployees = userInput.nextInt();
List<List<String>> employesInfo = new ArrayList<List<String>>();
for( int i = 0; i < numEmployees; i++ )
{
Scanner input = new Scanner(System.in);
System.out.println("Enter employees first name: ");
String firstName = input.nextLine();
System.out.println("Enter employees last name: ");
String lastName = input.nextLine();
System.out.println("Enter street employee lives on:");
String street = input.nextLine();
System.out.println("Enter city employee lives in:");
String city = input.nextLine();
System.out.println("Enter state employee lives in:");
String state = input.nextLine();
System.out.println("Enter employee's zip code:");
String zip = input.nextLine();
System.out.println("Enter month employee was hired:");
String month = input.nextLine();
System.out.println("Enter day employee was hired:");
String day = input.nextLine();
System.out.println("Enter year employee was hired:");
String year = input.nextLine();
Name name = new Name(firstName, lastName);
Address address = new Address(street, city, state, zip);
Date date = new Date(month, day, year);
employeeName = name.getName();
employeeAddress = address.getAddress();
hireDate = date.getDate();
ArrayList<String> obj = new ArrayList<String>();
obj.add(employeeName);
obj.add(employeeAddress);
obj.add(hireDate);
employeesInfo.add(obj);
}
for (int i = 0; i < numEmployees; i++ ) {
System.out.println(emploeesInfo.get(i));
}
}