1

I am trying to take input from the user on student data. First, I ask the user how many students they are inputing data on. Then, the code asks the user for the data on the exact number of students the user put in for the first question.

Below is the start of my code. I am having issues in getting the user input after the initial variable. I need to take that variable, say the user enters 5, I need to prompt the user 5 times to input the Students Name and grade. Like so:

Student 1 last name:
Student 1 first name:
Student 1 grade:

Student 2 last name:

I have to use an array, I just need to figure out how to properly get the user input.

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();
        }
    }
}
4

3 回答 3

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 ();
}
于 2013-03-02T16:31:18.937 回答
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);
}
于 2013-03-02T16:53:48.627 回答
0

我建议您使用 arrayList 来存储学生对象。请考虑以下示例以更好地理解:

首先,您可以创建一个模型类来使用 getters() 和 setters() 存储学生详细信息。它必须看起来像这样:

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

希望这可以帮助..

谢谢..快乐编码...

于 2013-03-02T17:32:25.743 回答