gpt4 book ai didi

java - 使用空数组创建方法

转载 作者:行者123 更新时间:2023-12-01 10:02:41 27 4
gpt4 key购买 nike

感谢 stackoverflow 用户的一些在线帮助,这里是对注册和删除学生方法的编辑,以添加到学生数组设置中。现在只需让其他方法也能发挥作用即可。对这些的任何见解都会有所帮助,特别是对于这些点的速度和效率。

导入java.util.Scanner;

公开课学校{

private Student[] theStudents;

public School() {
this.theStudents = new Student[] { null };// needs to start out as empty

}

/*
* next two methods would allow a user to add or drop a student into the
* student array for the school Also with Enroll student, should be able to
* assign a class to the student, i.e. Calculas, history, etc
*/
public void enrollStudent(Student newStudent) {
Student newStudents[] = new Student[theStudents.length+1];
for (int i = 0; i < theStudents.length; i++)
{
newStudents[i] = theStudents[i];
}
newStudents[theStudents.length] = newStudent;
theStudents = newStudents;
}

public void dropStudent(Student dropStudent) {
Student newStudents[] = new Student[theStudents.length-1];
for (int i = 0; i > theStudents.length; i--)
{
newStudents[i] = theStudents[i];
}
newStudents[theStudents.length] = dropStudent;
}

// add Test Score for a student
public double addTestScore(String newStudent, double testScore) {
testScores[posi] = testScore;
}

/*
* count the number of students in a given class, not the school
*/
public int countClassSize(String course) {

}

// get average average score of a given class
public double averageAverageScore(String course) {

}

/*
* add a programming language to only CS students at the school Will need to
* use the instanceof for proper type casting
*/
public String addProgrammingLanguage(String studentName, String programLanguage) {

}

/*
* Count the number of students in the school that know a certain
* programming language, again will need to typecast properly
*/
public int numberThatKnowLanguage(String programLanguage) {

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String dropStudent, course;

System.out.println("Welcome to the School. Please select " + "Option 1 to enroll a regular student, \n "
+ "Option 2 to enroll a CompSci student, \n" + "Option 3 to drop a student, \n"
+ "Option 4 to add test score or programming language, or \n" + "Option 5 to perform class analytics.");
int Operation = input.nextInt();

/*
* Simple UI to add and drop students, will need to set the operation to
* call the add and drop students to fit them to the Student body array
* will need to make these two options loop until the user is satisfied
* with the size of the student body
*/
if (Operation == 1) {
System.out.println("Enter the # of regular students that you want to add to the school.");
int addStudents = input.nextInt();
/*
* Possibly create some type of input array to
* make it easier to enter the students' names
*/
System.out.println("Please enter the name and course of the student you are enrolling:");
Student newRegularStudent = (String) input.next();
course = input.next();
}

else if (Operation == 2) {
System.out.println("Enter the # of CompSci students that you want to add to the school.");
int addStudents = input.nextInt();
/*
* Possibly create some type of input array to make it easier to
* enter the students' names
*/
System.out.println("Please enter the name and course of the student you are enrolling:");
Student newCompSciStudent = (String) input.next();
course = input.next();
}

else if (Operation == 3) {
System.out.println("Enter the # of students that you want to drop from the school.");
int dropStudents = input.nextInt();
/*
* Possibly create some type of input array to make
* it easier to enter the students' names
*/
System.out.println("Please enter the name of the student you wish to drop from the school:");
dropStudent = (String) input.next();

/*
* After the first two operations, will need to build to the UI to
* call the other five methods, will need to make it into a
* while/for loop so user can continue to add information as needed.
*/
}

else if (Operation == 4) {
System.out.println("Enter the # for what you want to add to a student's records."
+ "Enter 1 to enter a test score\n " + "Enter 2 to enter a programming language, enter 2.");
int optionNum1 = input.nextInt();
/*
* Possibly create some type of input array
* to make it easier to enter the students' names
*/
if (optionNum1 == 1) {

} else if (optionNum1 == 2) {

}
}

else if (Operation == 5) {
System.out.println("This is the analytics section of this program.\n");

System.out.println("Enter the # for which of the following analytics options that you want performed: "
+ "Enter 1 to count the # of students for a particular class,\n "
+ "Enter 2 to calculate the average average score for all students take a particular course, or\n "
+ "Enter 3 to count the # of CompSciStudents.");
int optionNum2 = input.nextInt();
if (optionNum2 == 1) {
System.out.println("Enter the course name that you want to know the # of students for.");
course = input.next();
Student classSize;
classSize.countClassSize(course);
}

else if (optionNum2 == 2) {
System.out.println("Enter the course name that you want \n"
+ "to calculate the average average test value for.");
course = input.next();
Student testAvg;
testAvg.averageAverageScore(course);
}

else if (optionNum2 == 3) {
System.out.println("Count the # of CompSciStudents who know a \n"
+ "particular programming language by entering that language name now.");
String programLanguage = input.next();
CompSciStudent csStudent;
csStudent.knowsLanguage(programLanguage);
}
}
input.close();
}

}

最佳答案

我不知道这是否是最好的方法,但如果我是你,我只会创建一个大一号或小一号的新数组,并在每次添加或删除时使用旧数组中的值填充它值(value)。例如:

public void enrollStudent(Student newStudent) {
Students newStudents[] = new Student[theStudents.length+1];
for (int i = 0; i < theStudents.length; i++)
{
newStudents[i] = theStudents[i];
}
newStudents[theStudents.length] = newStudent;
theStudents = newStudents;
}

效率不是很高,但是嘿,听起来你并没有做任何需要 super 高效的事情。

关于java - 使用空数组创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686317/

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