簡體   English   中英

如何使用java將字符串數組或整數值從一個類傳遞給另一個類?

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

我創建了一個簡單的基於控制台的學生系統,基本上程序會詢問您是否要輸入學生姓名或查看您輸入的姓名列表。 輸出流程如下圖所示:

*******基於控制台的學生系統************
1. 輸入學生姓名
2. 學生名單
輸入選擇的數量:1
輸入要輸入的學生人數:3
********************************************
一號學生
輸入全名:Alpha Jones
********************************************
2號學生
輸入全名:Beta Jones
********************************************
3號學生
輸入全名:伽瑪瓊斯
您想返回菜單嗎?(是/否):是

*******基於控制台的學生系統************
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 類(這是主要方法所在的位置):

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() 方法取回數組的數量,但它也失敗了。

請分享一些關於如何解決這個問題的建議,或者如果有更好的方法,建議新的東西來實現我的目標。

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<Student> students = new ArrayList<>(); 到你的班級。 在每個輸入法的末尾,將 Student 對象添加到此列表中。 在 listStudents 中,不要創建本地學生,而是打印您存儲在此列表中的學生。

根據您當前的系統,首先您可以創建一個全局變量 Student[] 或 List,然后在 'input Student Name()' 方法的末尾保存您的數據。 另一種可以使用數據庫來保存數據的方法,但這對您的系統來說不是必需的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM