Java 显示数组中的列表,以数字形式输入数据
我正在尝试从用户那里获取关于学生数据的输入。首先,我询问用户他们正在输入多少学生的数据。然后,代码要求用户提供关于用户为第一个问题输入的确切学生人数的数据 下面是我的代码的开始。我在获取初始变量后的用户输入时遇到问题。我需要取那个变量,假设用户输入Java 显示数组中的列表,以数字形式输入数据,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,我正在尝试从用户那里获取关于学生数据的输入。首先,我询问用户他们正在输入多少学生的数据。然后,代码要求用户提供关于用户为第一个问题输入的确切学生人数的数据 下面是我的代码的开始。我在获取初始变量后的用户输入时遇到问题。我需要取那个变量,假设用户输入5,我需要提示用户5次输入学生姓名和成绩。像这样: Student 1 last name: Student 1 first name: Student 1 grade: Student 2 last name: 我必须使用数组,我只需要弄清楚如何
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();
}
}
}
import java.util.Scanner;
公营班级学生成绩{
公共静态分数=新分数();
私人静态学生;
公共静态void main(字符串[]args){
System.out.println(“欢迎使用学生成绩应用程序。\n”);
getStudentScores();
}
公共静态无效getStudentScores(){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入要输入的学生人数:”);
int num=input.nextInt();
int[]a=新的int[num];
for(int i=0;i
String[]lastNames=新字符串[num];
String[]firstNames=新字符串[num];
int[]等级=新int[num];
for(int i=0;i
在我看来,处理阵列之间的关联不是一个好的做法,无论如何,由您决定您的设计。如果你想这么做,那么@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);
}
StudentScore[]studentScores=新的StudentScore[num];
for(int i=0;i
我建议您使用arrayList来存储Student对象。考虑下面的例子以更好地理解:
首先,可以使用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;
}
}
接下来,您可以创建StudentScoresAP,如下所示,以读取用户的输入:
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);
}
}
希望这有帮助
谢谢..快乐编码…我有问题:哪些问题?另外,您计划如何将姓氏存储到int中?
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);
}
}
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