【问题标题】:How to pass a string array or integer value from a class to another using java?java - 如何使用java将字符串数组或整数值从一个类传递给另一个类?
【发布时间】:2019-12-11 08:56:26
【问题描述】:

我创建了一个简单的基于控制台的学生系统,基本上程序会询问您是否要输入学生姓名或查看您输入的姓名列表。输出的流程如下图:

************基于控制台的学生系统************
1.输入学生姓名
2. 学生名单
输入选择的数量:1
输入要输入的学生人数:3
************************************************
1号学生
输入全名:阿尔法·琼斯
************************************************
2号学生
输入全名:Beta Jones
************************************************
3号学生
输入全名:Gamma Jones
你想回到菜单吗?(y/n): y

************基于控制台的学生系统************
1.输入学生姓名
2. 学生名单
输入选择的数量:2
******学生名单******



首先,我输入了三个新学生的名字 Alpha Jones、Beta Jones 和 Gamma Jones,但是当我选择查看所有名字时,一切都为空。这三个名字应该会出现。

这是学生类的代码:

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;
}
}

这是我设置将从 PracticeOne 类传递的值的地方,稍后,我将返回该值以在 PracticeOne 类中显示。

这里是 PracticeOne 类(这里是 main 方法所在的地方):

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());
        }

}
}

首先,我在 inputStudentName() 方法中调用了 Student 的一个实例,它传递了一个参数作为全名和正在使用的数组的数量,并尝试将其传递给 Student 类的构造函数,以便稍后从那里获取值。

在 listStudents 方法中,我尝试通过调用 Student 类的 getList() 方法来显示它,以取回之前输入的名称,但一切都是 NULL。我还尝试通过 getNumStudents() 方法取回数组的数量,但它也失败了。

请分享一些关于如何解决此问题的建议,或者如果有更好的方法,请提出新的建议来实现我的目标。

【问题讨论】:

  • 您在inputStudentName 中创建了一个新的Student,但永远不会重新使用它...
  • @Ward 它不必被退回,它应该被存储。如果你让它返回它,任何以前读取的学生数据都将被覆盖

标签: java arrays class constructor


【解决方案1】:
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());
        }

}

这是您的 listStudents 逻辑。您只需创建一个新的 Student 实例,而不是使用您已经创建的数据。这不包含任何信息是很正常的。

在您的输入法中,您也只将数据保存在局部变量中。这意味着,一旦方法完成,数据就会丢失。

将静态List&lt;Student&gt; students = new ArrayList&lt;&gt;(); 添加到您的班级。在每个输入法的末尾,将 Student 对象添加到此列表中。 在 listStudents 中,不要创建本地学生,而是打印您存储在此 List 中的学生。

【讨论】:

  • 学生班也有点偏。他在一个学生实例中存储了多个名称,并覆盖了 numInput。仅将他的学生存储在列表中可能无法解决他的问题。
  • @JuliusHörger 实际上,它会的。它只会在代码中创建一个逻辑......不合逻辑,但这个问题已经存在。一旦他将他的学生存储在列表中,在 hist listStudents 中,他需要首先迭代列表,然后为每个学生实例执行他现在拥有的打印。这将是混乱的代码,应该重构,但是当提出最小的更改时,OP 更有可能理解它们。
【解决方案2】:

根据您当前的系统,首先您可以创建一个全局变量 Student[] 或 List,然后在 'input Student Name()' 方法的末尾保存您的数据。 您可以使用数据库保存数据的另一种方式,但这对您的系统来说不是必需的。

【讨论】:

  • Java 不支持全局变量。创建一个 Student[] 类型的数组可能很危险,因为您需要预先知道要阅读的学生人数,或者每次超出限制时都需要创建一个新数组。
猜你喜欢
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多