简体   繁体   English

如何使用java将字符串数组或整数值从一个类传递给另一个类?

[英]How to pass a string array or integer value from a class to another using java?

I created a simple console based student system where basically the program will ask if you want to input name of students or view the list of names you inputted.我创建了一个简单的基于控制台的学生系统,基本上程序会询问您是否要输入学生姓名或查看您输入的姓名列表。 The flow of the output is as shown below:输出流程如下图所示:

*******CONSOLE BASED STUDENT SYSTEM************ *******基于控制台的学生系统************
1. Input Student Name 1. 输入学生姓名
2. List of students 2. 学生名单
Enter the number of choice: 1输入选择的数量:1
Enter number of students to input: 3输入要输入的学生人数:3
**************************************** ********************************************
Student No. 1一号学生
Enter full name: Alpha Jones输入全名:Alpha Jones
**************************************** ********************************************
Student No. 2 2号学生
Enter full name: Beta Jones输入全名:Beta Jones
**************************************** ********************************************
Student No. 3 3号学生
Enter full name: Gamma Jones输入全名:伽玛琼斯
Do you wish to proceed back to menu?(y/n): y您想返回菜单吗?(是/否):是

*******CONSOLE BASED STUDENT SYSTEM************ *******基于控制台的学生系统************
1. Input Student Name 1. 输入学生姓名
2. List of students 2. 学生名单
Enter the number of choice: 2输入选择数量:2
******LIST OF STUDENTS****** ******学生名单******
NULL空值
NULL空值
NULL空值

Firstly, I input three new students names Alpha Jones, Beta Jones and Gamma Jones but when I chose to view all names, everything is null.首先,我输入了三个新学生的名字 Alpha Jones、Beta Jones 和 Gamma Jones,但是当我选择查看所有名字时,一切都为空。 The three names should appear.这三个名字应该会出现。

Here is the code for Student Class:这是学生类的代码:

public class Student {
private String[] names;
private int numInput;

public Student(){

}

public Student(String[] fullName, int nI){
    numInput = nI;
    names = new String[nI];
    for(int index = 0; index < nI; index++){
        names[index] = fullName[index];
    }
}

public String[] getList(){
    return names;
}

public int getNumStudents(){
    return numInput;
}
}

This is where I setup the values that will be passed on from the PracticeOne class, and later on, I will return that value back for display in PracticeOne Class.这是我设置将从 PracticeOne 类传递的值的地方,稍后,我将返回该值以在 PracticeOne 类中显示。

Here is the PracticeOne Class(This is where the main method is located):这是 PracticeOne 类(这是主要方法所在的位置):

import java.util.Scanner;
public class PracticeOne {
public static void main(String[]args){
    Scanner hold = new Scanner(System.in);
    int menuNumChoice;

    String response;
    do{

        System.out.println("******CONSOLE BASED STUDENT SYSTEM******");
        System.out.println("1. Input Student Name");
        System.out.println("2. List of Students");
        System.out.print("Enter the number of choice: ");
        menuNumChoice = hold.nextInt();

        switch(menuNumChoice){
            case 1:
                inputStudentName(hold);
                break;
            case 2:
                listStudents();
                break;
            default:
                System.out.println("ERROR 101");
                break;
        }

        System.out.print("Do you wish to proceed?(y/n): ");
        response = hold.nextLine();

    }while(response.equalsIgnoreCase("y"));
}

public static void inputStudentName(Scanner hold){
    int numInput;


    System.out.print("Enter number of students to input: ");
    numInput = hold.nextInt();

    hold.nextLine();
    String[] fullName = new String[numInput];

    for(int x = 0; x < numInput; x++){

        System.out.println("***************************");
        System.out.println("Student No. " + (x + 1));
        System.out.print("Enter full name: ");
        fullName[x] = hold.nextLine();
        System.out.println();

    }

    Student s = new Student(fullName,numInput);

}

public static void listStudents(){
        Student s = new Student();
        System.out.println("******LIST OF STUDENTS******");
        for(int y = 0; y < s.getNumStudents();y++){
            System.out.println(s.getList());
        }

}
}

Firstly I called an instance of Student in inputStudentName() method, which passes an argument for the fullname and the number of arrays being used, and tries to transfer it to Student class constructor, to get the values from there later on.首先,我在 inputStudentName() 方法中调用了 Student 的一个实例,该方法传递了一个参数作为全名和正在使用的数组的数量,并尝试将其传输到 Student 类构造函数,以便稍后从那里获取值。

In the listStudents method, I tried displaying it by calling the getList() method from Student class to supposedly get back the names that was inputted earlier but everything was NULL.在 listStudents 方法中,我尝试通过从 Student 类调用 getList() 方法来显示它,以获取之前输入的名称,但一切都是 NULL。 I also tried getting back the number of arrays through the getNumStudents() method but it also failed.我也尝试通过 getNumStudents() 方法取回数组的数量,但它也失败了。

Kindly share some advice on how to work around with this problem or if theres a better way, suggest new things to achieve my goal.请分享一些关于如何解决这个问题的建议,或者如果有更好的方法,建议新的东西来实现我的目标。

public static void listStudents(){
        **Student s = new Student();**
        System.out.println("******LIST OF STUDENTS******");
        for(int y = 0; y < s.getNumStudents();y++){
            System.out.println(s.getList());
        }

}

This is your listStudents logic.这就是你的 listStudents 逻辑。 Instead of using the data you already created, you just create a new instance of Student.您只需创建一个 Student 的新实例,而不是使用您已经创建的数据。 It's quite normal that this doesn't contain any information.这不包含任何信息是很正常的。

In your input method, you also only save the data in a local variable.在您的输入法中,您也仅将数据保存在局部变量中。 This means, that once the method is finished, the data is lost.这意味着,一旦方法完成,数据就会丢失。

Add a static List<Student> students = new ArrayList<>();添加一个静态List<Student> students = new ArrayList<>(); to your class.到你的班级。 At the end of each input method, add the Student object to this list.在每个输入法的末尾,将 Student 对象添加到此列表中。 In listStudents, don't create a local student, but print the ones you stored in this List.在 listStudents 中,不要创建本地学生,而是打印您存储在此列表中的学生。

Based on your current system,first you can create a global variable Student[] or List then at the end of ' input Student Name()' method to save your dat.根据您当前的系统,首先您可以创建一个全局变量 Student[] 或 List,然后在 'input Student Name()' 方法的末尾保存您的数据。 Another Way you can use database to save your data,But this is not necessary for your system.另一种可以使用数据库来保存数据的方法,但这对您的系统来说不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM