我正在尝试从用户那里获取学生数据的输入。首先,我问用户他们输入数据的学生数量。然后,代码询问用户有关第一个问题的用户投入的确切学生数的数据。
以下是我的代码的开头。我在初始变量后获取用户输入时遇到问题。我需要接受该变量,比如用户输入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();
}
}
}
答案 0 :(得分:1)
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 ();
}
答案 1 :(得分:1)
在我看来,处理数组之间的关联不是一个好习惯,无论如何,由你来决定你的设计。如果你想这样做,那么@Mikhail Vladimirov的建议是要走的路。
另一方面,只需根据需要设计一个类,并将类的对象存储在数组或列表中。
public class StudentScore{
String firstName;
String lastName;
int grade;
pulbic StudnetScore(String firstName, String lastName, int grade){
this.firstName = firstName;
this.lastName = lastName;
this.grade = grade;
}
//getters(), setters()
}
在主要课程中:
StudentScore[] studentScores = new StudentScore[num];
for (int i = 0; i < studentScores.length; i++){
System.out.print ("Enter Student " + (i + 1) + " last name:");
String lastName = in.nextLine ();
System.out.print ("Enter Student " + (i + 1) + " first name:");
String firstName = in.nextLine ();
System.out.print ("Enter Student " + (i + 1) + " grade:");
int grade = in.nextInt ();
studentScores[i] = new StudentScore(firstName,lastName,grade);
}
答案 2 :(得分:0)
我建议您使用arrayList存储Student对象。考虑以下示例以便更好地理解:
首先,您可以使用getters()&amp;创建一个模型类来存储学生的详细信息。制定者()。它必须看起来像这样:
package com.stack.overflow.works.model;
public class Student {
private String firstName;
private String lastName;
private int score;
public Student() {}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
接下来,您可以创建StudentScoresApp,如下所示,以阅读用户的输入:
package com.stack.overflow.works.main;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.stack.overflow.works.model.Student;
public class StudentScoresApp {
public static List<Student> getStudentScores() {
List<Student> students = new ArrayList<Student>();
Student student = null;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students to enter: ");
int numberOfStudents = scanner.nextInt();
for (int i = 0; i < numberOfStudents; i++) {
student = new Student();
System.out.print("Enter Student " + (i + 1) + " First Name:");
String firstName = scanner.next();
student.setFirstName(firstName);
System.out.print("Enter Student " + (i + 1) + " Last Name:");
String lastName = scanner.next();
student.setLastName(lastName);
System.out.print("Enter Student " + (i + 1) + " Score:");
int score = scanner.nextInt();
student.setScore(score);
students.add(student);
}
scanner.close();
return students;
}
public static void displayStudentScores(List<Student> students) {
int i = 1;
for (Student student: students) {
System.out.println("Student " + (i) + " First Name:" + student.getFirstName());
System.out.println("Student " + (i) + " Last Name:" + student.getLastName());
System.out.println("Student " + (i) + " Score:" + student.getScore());
i++;
}
}
public static void main(String[] args) {
System.out.println("Welcome to the Student Scores Application");
System.out.println("*****************************************");
List<Student> students = StudentScoresApp.getStudentScores();
System.out.println();
System.out.println("Displaying Student Scores:");
System.out.println("*************************");
StudentScoresApp.displayStudentScores(students);
}
}
现在,您可以运行StudentScoresApp。样品测试结果如下所示:
Welcome to the Student Scores Application
*****************************************
Enter number of students to enter: 3
Enter Student 1 First Name:Sandeep
Enter Student 1 Last Name:Thulaseedharan
Enter Student 1 Score:100
Enter Student 2 First Name:Sathya
Enter Student 2 Last Name:Narayanan
Enter Student 2 Score:100
Enter Student 3 First Name:Jayakrishnan
Enter Student 3 Last Name:Lal
Enter Student 3 Score:100
Displaying Student Scores:
*************************
Student 1 First Name:Sandeep
Student 1 Last Name:Thulaseedharan
Student 1 Score:100
Student 2 First Name:Sathya
Student 2 Last Name:Narayanan
Student 2 Score:100
Student 3 First Name:Jayakrishnan
Student 3 Last Name:Lal
Student 3 Score:100
希望这会有所帮助..
谢谢..快乐编码......