每个框包含多个信息的数组
Arrays With Multiple Information Per Box
所以我有这个练习题,我应该在其中创建一个包含员工信息的数组并将其传递给 class;我的代码有问题,我似乎无法弄清楚。
代码的目的是:
将代码中看到的信息放入数组中,然后传递给 class 中的方法,然后打印出来给用户。 (class 中的代码非常好,因此这里没有包含它)。
如果有人能提供帮助,那就太好了。
谢谢。
//代码。
// The Scanners.
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = input.nextInt();
//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];
// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i++){
System.out.println("Enter employee " + i + "'s name: ");
String name = scan.nextLine();
System.out.println("Enter employee " + i + "'s birth date: ");
String bday = scan.nextLine();
System.out.println("Enter employee " + i + "'s salary: ");
double salary = input.nextDouble();
System.out.println("Enter employee " + i + "'s overtime: ");
int overtime = input.nextInt();
E[i] = (name, bday, salary, overtime);
}
System.out.println("Employee's Information"
+ "\n----------------------"
+ "\n----------------------");
for(int i = 0; i <= E.length-1; i++){
E[i].print();
}
}
这里有两个问题需要解决。
首先,不要创建多个 Scanner
对象,从 System.in
读取。我知道你这样做是因为这个问题(检查出来):Scanner is skipping nextLine() after using next() or nextFoo()? - 但是除了创建另一个 Scanner
.
之外还有另一种解决方案
第二期在这里:
E[i] = (name, bday, salary, overtime);
您有一个 Employee[]
正在尝试填写。但是你的语法错误。你实际上想创建一个 new Employee(...)
来填充你的数组。
所以这一行应该是正确的(前提是 Employee
有一个给定类型的构造函数):
E[i] = new Employee(name, bday, salary, overtime);
在您的代码片段中考虑这一点时:
Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = scan.nextInt();
scan.nextLine(); // <- reads the newline from the console
//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];
// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i++){
System.out.println("Enter employee " + i + "'s name: ");
String name = scan.nextLine();
System.out.println("Enter employee " + i + "'s birth date: ");
String bday = scan.nextLine();
System.out.println("Enter employee " + i + "'s salary: ");
double salary = scan.nextDouble();
System.out.println("Enter employee " + i + "'s overtime: ");
int overtime = scan.nextInt();
// create a new employee with the entered information and save in the array
E[i] = new Employee(name, bday, salary, overtime);
}
我不确定双扫描仪。
显而易见的是,由于您定义了一个 Employee 对象类型的空数组,该数组只能容纳 Employee 对象。
Employee[] E = new Employee[numberOfEmployees]
要定义一个对象,需要为其分配一块内存,如:
new Employee(name, bday, salary, overtime);
然后赋值到数组E
E[i] = new Employee(name, bday, salary, overtime);
我希望把事情搞清楚。
所以我有这个练习题,我应该在其中创建一个包含员工信息的数组并将其传递给 class;我的代码有问题,我似乎无法弄清楚。
代码的目的是: 将代码中看到的信息放入数组中,然后传递给 class 中的方法,然后打印出来给用户。 (class 中的代码非常好,因此这里没有包含它)。
如果有人能提供帮助,那就太好了。
谢谢。
//代码。
// The Scanners.
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = input.nextInt();
//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];
// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i++){
System.out.println("Enter employee " + i + "'s name: ");
String name = scan.nextLine();
System.out.println("Enter employee " + i + "'s birth date: ");
String bday = scan.nextLine();
System.out.println("Enter employee " + i + "'s salary: ");
double salary = input.nextDouble();
System.out.println("Enter employee " + i + "'s overtime: ");
int overtime = input.nextInt();
E[i] = (name, bday, salary, overtime);
}
System.out.println("Employee's Information"
+ "\n----------------------"
+ "\n----------------------");
for(int i = 0; i <= E.length-1; i++){
E[i].print();
}
}
这里有两个问题需要解决。
首先,不要创建多个 Scanner
对象,从 System.in
读取。我知道你这样做是因为这个问题(检查出来):Scanner is skipping nextLine() after using next() or nextFoo()? - 但是除了创建另一个 Scanner
.
第二期在这里:
E[i] = (name, bday, salary, overtime);
您有一个 Employee[]
正在尝试填写。但是你的语法错误。你实际上想创建一个 new Employee(...)
来填充你的数组。
所以这一行应该是正确的(前提是 Employee
有一个给定类型的构造函数):
E[i] = new Employee(name, bday, salary, overtime);
在您的代码片段中考虑这一点时:
Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = scan.nextInt();
scan.nextLine(); // <- reads the newline from the console
//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];
// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i++){
System.out.println("Enter employee " + i + "'s name: ");
String name = scan.nextLine();
System.out.println("Enter employee " + i + "'s birth date: ");
String bday = scan.nextLine();
System.out.println("Enter employee " + i + "'s salary: ");
double salary = scan.nextDouble();
System.out.println("Enter employee " + i + "'s overtime: ");
int overtime = scan.nextInt();
// create a new employee with the entered information and save in the array
E[i] = new Employee(name, bday, salary, overtime);
}
我不确定双扫描仪。
显而易见的是,由于您定义了一个 Employee 对象类型的空数组,该数组只能容纳 Employee 对象。
Employee[] E = new Employee[numberOfEmployees]
要定义一个对象,需要为其分配一块内存,如:
new Employee(name, bday, salary, overtime);
然后赋值到数组E
E[i] = new Employee(name, bday, salary, overtime);
我希望把事情搞清楚。