gpt4 book ai didi

java - 使用方法和数组的问题

转载 作者:行者123 更新时间:2023-11-30 04:05:33 25 4
gpt4 key购买 nike

我的问题是使用另一种方法显示数据。我尝试了两种方法,但没有输出。

我认为当我将 ArrayList 中的对象作为两个方法的参数时,它们就消失了,也可能没有。

请帮我解决我的这个问题。还有更多选项需要填写,我还需要一些帮助。

public class Student {
private String IDNumber;
private String firstName;
private String middleName;
private String lastName;
private String degree;
private int yearLevel;

public Student() {
this.IDNumber = IDNum;
this.firstName = fName;
this.middleName = mName;
this.lastName = lName;
this.degree = deg;
this.yearLevel = level;
}

public void setIdNumber(String IDNumber) {
this.IDNumber = IDNumber;
}

public String getIdNumber() {
return IDNumber;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

public void setMiddleName(String middleName) {
this.middleName = middleName;
}

public String getMiddleName()
{
return middleName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}

public void setDegree(String degree) {
this.degree = degree;
}

public String getDegree() {
return degree;
}

public void setYearLevel(int yearLevel) {
this.yearLevel = yearLevel;
}

public int getYearLevel() {
return yearLevel;
}

/* @Override
public String toString(){
return ("ID Number: "+this.getIdNumber()+
"\nName: "+ this.getFirstName()+
" "+ this.getMiddleName()+
" "+ this.getLastName()+
"\nDegree and YearLevel: "+ this.getDegree() +
" - " + this.getYearLevel());
} */

}
import java.util.ArrayList;
import java.util.Scanner;

public class test {

public static void main(String[] args) {
menu();
}

public static void menu() {
Scanner in = new Scanner(System.in);

int choice = 0;
System.out.print("****STUDENT RECORD SYSTEM****\n\n");
System.out.println("\t MENU ");
System.out.println("[1]ADD STUDENT");
System.out.println("[2]DISPLAY ALL");
System.out.println("[3]DISPLAY SPECIFIC");
System.out.println("[4]UPDATE");
System.out.println("[5]AVERAGE");
System.out.println("[6]EXIT");
System.out.println("?");

choice = in.nextInt();
if (choice == 1) {
options();
}

else if (choice == 2) {
displayAll(student, studentList);
}

else if (choice == 3) {
displaySpecific(student, studentList);
}

}

public static void options() {
Scanner in = new Scanner(System.in);
ArrayList<Student> studentList = new ArrayList<Student>();
char ans;
String temp;

int total;

do {

System.out.println("TOTAL: ");
total = in.nextInt();

Student[] student = new Student[total];

for (int index = 0; index < student.length; index++) {
student[index] = new Student();

System.out.print("*********STUDENT INFORMATION*********\n\n");
System.out.println("PRESS ENTER");
in.nextLine();
System.out.print("ID NUMBER: ");
student[index].setIdNumber(in.nextLine());

System.out.print("FIRST NAME: ");
student[index].setFirstName(in.nextLine());

System.out.print("MIDDLE NAME: ");
student[index].setMiddleName(in.nextLine());

System.out.print("LAST NAME: ");
student[index].setLastName(in.nextLine());

System.out.print("DEGREE: ");
student[index].setDegree(in.nextLine());

System.out.print("YEAR LEVEL: ");
student[index].setYearLevel(in.nextInt());

studentList.add(student[index]);

}

System.out
.print("Would you like to enter in a new student (y/n)? ");
String answer = in.next();
ans = answer.charAt(0);

} while (ans == 'y');

// SEARCH and DISPLAY SPECIFIC
String id = new String();
in.nextLine();
System.out.print("Enter ID NUMBER: ");
id = in.nextLine();

for (int j = 0; j < studentList.size(); j++) {
if (id.equals(studentList.get(j).getIdNumber())) {
System.out.printf("STUDENT SEARCHED");
System.out.print("\nID NUMBER: "
+ studentList.get(j).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(j).getFirstName() + " "
+ studentList.get(j).getMiddleName() + " "
+ studentList.get(j).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(j).getDegree() + "-"
+ studentList.get(j).getYearLevel() + "\n\n");
System.out.println();
}
}

// DISPLAY ALL
for (int i = 0; i < studentList.size(); i++) {
System.out.printf("STUDENT[%d]", i + 1);
System.out
.print("\nID NUMBER: " + studentList.get(i).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(i).getFirstName() + " "
+ studentList.get(i).getMiddleName() + " "
+ studentList.get(i).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(i).getDegree() + "-"
+ studentList.get(i).getYearLevel());
System.out.println();
}

menu();

}

public static void displayAll(Student student,
ArrayList<Student> studentList) {
System.out.printf("STUDENT RECORD");

for (int i = 0; i < studentList.size(); i++) {
System.out.printf("STUDENT[%d]", i + 1);
System.out
.print("\nID NUMBER: " + studentList.get(i).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(i).getFirstName() + " "
+ studentList.get(i).getMiddleName() + " "
+ studentList.get(i).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(i).getDegree() + "-"
+ studentList.get(i).getYearLevel());
System.out.println();
}
}

public static void displaySpecific(Student student,
ArrayList<Student> studentList) {
String id = new String();
in.nextLine();
System.out.print("Enter ID NUMBER: ");
id = in.nextLine();

for (int j = 0; j < studentList.size(); j++) {
if (id.equals(studentList.get(j).getIdNumber())) {
System.out.printf("STUDENT SEARCHED");
System.out.print("\nID NUMBER: "
+ studentList.get(j).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(j).getFirstName() + " "
+ studentList.get(j).getMiddleName() + " "
+ studentList.get(j).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(j).getDegree() + "-"
+ studentList.get(j).getYearLevel() + "\n\n");
System.out.println();
}
}
}
}

最佳答案

移动

ArrayList <Student> studentList = new ArrayList <Student>();

options方法到类字段。

编辑:

正如你所看到的,现在studentList不是一个局部变量

public static void options()

但属于类(class)。

无论如何,我编辑了一些方法的参数,因为你不需要将学生作为参数传递,因为现在它是类(class)的一个字段。

import java.util.ArrayList;
import java.util.Scanner;

public class test {
static ArrayList<Student> studentList = new ArrayList<Student>();

public static void main(String[] args) {
menu();
}

public static void menu() {
Scanner in = new Scanner(System.in);

int choice = 0;
System.out.print("****STUDENT RECORD SYSTEM****\n\n");
System.out.println("\t MENU ");
System.out.println("[1]ADD STUDENT");
System.out.println("[2]DISPLAY ALL");
System.out.println("[3]DISPLAY SPECIFIC");
System.out.println("[4]UPDATE");
System.out.println("[5]AVERAGE");
System.out.println("[6]EXIT");
System.out.println("?");

choice = in.nextInt();
if (choice == 1) {
options();
}

else if (choice == 2) {
displayAll();
}

else if (choice == 3) {
displaySpecific(student);// here you should ask to the user what studend he want to show - here it continues to give you error
}

}

public static void options() {
Scanner in = new Scanner(System.in);
char ans;
String temp;

int total;

do {

System.out.println("TOTAL: ");
total = in.nextInt();

Student[] student = new Student[total];

for (int index = 0; index < student.length; index++) {
student[index] = new Student();

System.out.print("*********STUDENT INFORMATION*********\n\n");
System.out.println("PRESS ENTER");
in.nextLine();
System.out.print("ID NUMBER: ");
student[index].setIdNumber(in.nextLine());

System.out.print("FIRST NAME: ");
student[index].setFirstName(in.nextLine());

System.out.print("MIDDLE NAME: ");
student[index].setMiddleName(in.nextLine());

System.out.print("LAST NAME: ");
student[index].setLastName(in.nextLine());

System.out.print("DEGREE: ");
student[index].setDegree(in.nextLine());

System.out.print("YEAR LEVEL: ");
student[index].setYearLevel(in.nextInt());

studentList.add(student[index]);

}

System.out
.print("Would you like to enter in a new student (y/n)? ");
String answer = in.next();
ans = answer.charAt(0);

} while (ans == 'y');

// SEARCH and DISPLAY SPECIFIC
String id = new String();
in.nextLine();
System.out.print("Enter ID NUMBER: ");
id = in.nextLine();

for (int j = 0; j < studentList.size(); j++) {
if (id.equals(studentList.get(j).getIdNumber())) {
System.out.printf("STUDENT SEARCHED");
System.out.print("\nID NUMBER: "
+ studentList.get(j).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(j).getFirstName() + " "
+ studentList.get(j).getMiddleName() + " "
+ studentList.get(j).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(j).getDegree() + "-"
+ studentList.get(j).getYearLevel() + "\n\n");
System.out.println();
}
}

// DISPLAY ALL
for (int i = 0; i < studentList.size(); i++) {
System.out.printf("STUDENT[%d]", i + 1);
System.out
.print("\nID NUMBER: " + studentList.get(i).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(i).getFirstName() + " "
+ studentList.get(i).getMiddleName() + " "
+ studentList.get(i).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(i).getDegree() + "-"
+ studentList.get(i).getYearLevel());
System.out.println();
}

menu();

}

public static void displayAll() {

for (int i = 0; i < studentList.size(); i++) {
System.out.printf("STUDENT[%d]", i + 1);
System.out
.print("\nID NUMBER: " + studentList.get(i).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(i).getFirstName() + " "
+ studentList.get(i).getMiddleName() + " "
+ studentList.get(i).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(i).getDegree() + "-"
+ studentList.get(i).getYearLevel());
System.out.println();
}
}

public static void displaySpecific(Student student) {
String id = new String();
in.nextLine();
System.out.print("Enter ID NUMBER: ");
id = in.nextLine();

for (int j = 0; j < studentList.size(); j++) {
if (id.equals(studentList.get(j).getIdNumber())) {
System.out.printf("STUDENT SEARCHED");
System.out.print("\nID NUMBER: "
+ studentList.get(j).getIdNumber());
System.out.print("\nFULL NAME: "
+ studentList.get(j).getFirstName() + " "
+ studentList.get(j).getMiddleName() + " "
+ studentList.get(j).getLastName());
System.out.print("\nDEGREE and YEAR: "
+ studentList.get(j).getDegree() + "-"
+ studentList.get(j).getYearLevel() + "\n\n");
System.out.println();
}
}
}
}

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

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