我是编码和Java的新手,编写了一个程序,该程序可以打印工人姓名,姓氏和年龄的列表。在执行第二个“ for”循环时,程序同时请求两个输入(名称和姓氏)。该如何解决?
如果对代码,语法,类的使用有任何批评,请对此发表评论。谢谢。
import java.util.Scanner;
public class Firm {
public static void main(String[] args) {
class Worker {
private int n, age;
private String name, surn;
private String[] names = new String[10];
private String[] surnames = new String[10];
private int[] years = new int[10];
public void workers(String name, String surn, int age, int index) {
names[index] = name;
surnames[index] = surn;
years[index] = age;
}
public String showme(int index) {
return(names[index] + " " + surnames[index]+ " " + years[index]);
}
}
Scanner scanner = new Scanner(System.in);
Worker worker = new Worker();
System.out.println("How many workers do you want to show: ");
worker.n = scanner.nextInt();
for(int i=0;i<worker.n;i++) {
System.out.println("Name: ");
worker.name = scanner.nextLine();
System.out.println("Surname: ");
worker.surn = scanner.nextLine();
System.out.println("Age: ");
worker.age = scanner.nextInt();
worker.workers(worker.name, worker.surn, worker.age, i);
}
System.out.println("Current workers are: ");
for(int i=0; i<worker.n; i++) {
System.out.println(worker.showme(i));
}
}
}