我正在尝试获取用户对学生数据的输入。首先,我询问用户他们正在输入多少学生的数据。然后,代码会要求用户提供有关用户在第一个问题中输入的确切学生人数的数据。
下面是我的代码的开头。我在初始变量之后获取用户输入时遇到问题。我需要采用该变量,假设用户输入 5
,我需要提示用户 5 次输入学生姓名和成绩。就像这样:
Student 1 last name:
Student 1 first name:
Student 1 grade:
Student 2 last name:
我必须使用数组,我只需要弄清楚如何正确获取用户输入。
import java.util.Scanner;
public class StudentScoresApp {
public static Score score = new Score();
private static Student student;
public static void main(String[] args) {
System.out.println("Welcome to the Student Scores Application.\n");
getStudentScores();
}
public static void getStudentScores() {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of students to enter: ");
int num = input.nextInt();
int [] a = new int[num];
for (int i = 0 ; i < num ; i++); {
System.out.print("Enter Student " + (i + 1) + " last name:");
a[i] = in.nextInt();
}
}
}
最佳答案
String [] lastNames = new String [num];
String [] firstNames = new String [num];
int [] grades = new int [num];
for (int i = 0; i < num; i++)
{
System.out.print ("Enter Student " + (i + 1) + " last name:");
lastNames [i] = in.nextLine ();
System.out.print ("Enter Student " + (i + 1) + " first name:");
firstNames [i] = in.nextLine ();
System.out.print ("Enter Student " + (i + 1) + " grade:");
gradess [i] = in.nextInt ();
}
关于java - 显示数组中的列表,以数字方式输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176699/