我正在做作业,老师要求我们存储100个以下的学生姓名和学生成绩。并且应该要求用户输入姓名和成绩。代码没有错误,但是当我运行文件时,它仅询问我一次名称。但是添加到成绩数组中不会有这样的问题吗?什么会导致这种情况发生?能够输入名称应该怎么做?
public class Assignment12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int grade[] = new int[3];
String name[] = new String[3];
for (int i = 0; i < 3; i++) {
System.out.println("Enter Student name");
String studentName = input.nextLine();
name[i] = studentName;
System.out.println("Enter Student grade");
int studentGrade = input.nextInt();
grade[i] = studentGrade;
}
}
}
答案 0 :(得分:0)
您可以在下面尝试此代码。
Scanner scan = new Scanner(System.in);
int[] marks = new int[3];
String[] names = new String[3];
//input marks
for(int i = 0; i < 3; i++) {
marks[i] = scan.nextInt();
}
//for Enter key character as input, in order to read the strings
scan.nextLine();
//loop to read names
for(int i = 0; i < 3; i++) {
names[i] = scan.nextLine();
}
//for printing out marks
for(int i = 0; i < 3; i++) {
System.out.println(marks[i]);
}
//for printing out names
for(int i = 0; i < 3; i++) {
System.out.println(names[i]);
}