gpt4 book ai didi

java - 将学生添加到数组中

转载 作者:行者123 更新时间:2023-12-02 05:09:58 24 4
gpt4 key购买 nike

我已经弄清楚如何从用户那里收集数据以在我的数组中创建一个新学生,但我无法将该信息添加到数组中。请告诉我如何在给定的代码中将此数据添加为新的 Student 对象。请引用AddStudent方法。

import java.util.Scanner;

public class ArrayDemo {

static Student[] students;

private static void ViewStudents() {

for (int i = 0; i < students.length; i++) {
System.out.println(i + ") " + students[i].getLName() + ", " + students[i].getFName());
}
}

private static void ViewDetails() {
Scanner kb = new Scanner(System.in);

int i;
System.out.println("Who would you like to view?");
ViewStudents();
i = Integer.parseInt(kb.nextLine());
System.out.println("ANum:\t\t" + students[i].getANum());
System.out.println("\nAddress:\t" + students[i].address.getHouseNum() + " " + students[i].address.getStreet());
System.out.println("\t\t" + students[i].address.getCity() + ", " + students[i].address.getState() + " " + students[i].address.getZip());
System.out.println("\t\t" + students[i].address.getLine2());
}

private static void AddStudent() {
Scanner kb = new Scanner(System.in);

Student student = new Student();

String FirstName;
String LastName;
int HouseNum;
String Street;
String City;
String State;
int Zip;
String Line2;

System.out.println("\tInput Information");
System.out.println("\tFirst Name:");
FirstName = kb.nextLine();
System.out.println("\tLast Name:");
LastName = kb.nextLine();
System.out.println("\tHouse Number:");
HouseNum = Integer.parseInt(kb.nextLine());
System.out.println("\tStreet:");
Street = kb.nextLine();
System.out.println("\tCity:");
City = kb.nextLine();
System.out.println("\tState:");
State = kb.nextLine();
System.out.println("\tZip Code:");
Zip = Integer.parseInt(kb.nextLine());
System.out.println("\tExtra Information:");
Line2 = kb.nextLine();

System.out.println("\nStudent:\t" + LastName + ", " + FirstName);
System.out.println("ANum:\t\t" + student.getANum());
System.out.println("Address:\t" + HouseNum + " " + Street);
System.out.println("\t\t" + City + ", " + State + " " + Zip);
System.out.println("\t\t" + Line2);

//students.setAddress( HouseNum, Street, City, State, Zip, Line2 );
System.out.println("\tYour Student was Successfully Added");
}

private static void RemoveStudent() {
Scanner kb = new Scanner(System.in);
System.out.println("Who would you like to remove?");
ViewStudents();

for (int j = Integer.parseInt(kb.nextLine()); j < students.length - 1; j++) {
students[j] = students[j + 1];
}

students[students.length - 1] = null;
}

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);

int x = 40;
//students = new Student[0];
students = new Student[2];

students[0] = new Student("Thomas", "Emily");
students[1] = new Student("Bob", "Joe");
students[0].address = new Address(6614, "White Sands ln", "Hixson", "Tennessee", 37343, "");
students[1].address = new Address(66, "White ln", "Hson", "Tealamabaee", 373873, "");

do {
System.out.println();
System.out.println("Do you want to:");
System.out.println("\t0) View Students");
System.out.println("\t1) View Students' Details");
System.out.println("\t2) Add a Student");
System.out.println("\t3) Remove a Student");
System.out.println("\t4) Exit");
x = Integer.parseInt(kb.nextLine());

switch (x) {

case 0:
ViewStudents();
break;
case 1:
ViewDetails();
break;
case 2:
AddStudent();
break;
case 3:
RemoveStudent();
break;
case 4:

break;
default:
}
} while (x != 4);
}
}

最佳答案

据我所知,您使用了错误的数据结构。您似乎认为添加动态数量的 StudentArrayList 会比 Array 更好、更合适。然后您可以简单地使用 add 方法。

请记住,Array 的大小是不可变的(它无法更改),因此在这种情况下您想要的肯定是某种 List,可能是 数组列表

如果您坚持使用数组,则需要跟踪已添加的学生人数并将其用作索引(作为全局变量或将其作为参数传递)。但请记住,数组的本质意味着您很快就会达到上限,除非您设置非常高的容量,

关于java - 将学生添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27415114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com