我不知道如何把它......
基本上,我们应该制作一个数组。我们输入学生人数,姓名和年龄。之后将显示输出。它将显示学生的姓名和年龄。
你看起来很容易。但我真的需要关于如何缩短产量的建议。就像我有什么办法可以做到最小化代码并创建无限数量的输出,以便我可以输入任意数量的学生。
public static void main(String[] args)
{
int ageInput;
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.print("Number of Students > ");
int studentValue = input.nextInt();
String[] arraylist = new String[studentValue];
int[] ageinput = new int[studentValue];
char count;
for (count = 0; count < studentValue; count++)
{
System.out.println("Please input a student name and age:");
System.out.println("------------------------------------");
System.out.println("Name: ");
arraylist[count] = input.next();
System.out.println("Age: ");
ageinput[count] = input2.nextInt();
System.out.println(" ");
}
for (String a: arraylist)
{
System.out.println("Student #1");
System.out.println("name: " +arraylist[0]);
System.out.println("age: " +ageinput[0]);
System.out.println("Student #2");
System.out.println("name: " +arraylist[1]);
System.out.println("age: " +ageinput[1]);
System.out.println("Student #3");
System.out.println("name: " +arraylist[2]);
System.out.println("age: " +ageinput[2]);
System.out.println("Student #4");
System.out.println("name: " +arraylist[3]);
System.out.println("age: " +ageinput[3]);
System.out.println("Student #5");
System.out.println("name: " +arraylist[4]);
System.out.println("age: " +ageinput[4]);
}
}
}
我对这些东西不是很擅长。如果你能回答这个问题,我真的很感激:)谢谢大家!
答案 0 :(得分:3)
为了使用循环打印结果,我建议您使用以下代码:
for(int i = 0; i <studentValue; i++)
{
System.out.println("Student #" + i + 1);
System.out.println("name: " + arraylist[i]);
System.out.println("age: " + ageinput[i]);
}