繁体   English   中英

分配值后,循环内的Getter返回null

[英]Getter within loop returning null after assigning values

我的程序有一个小问题。 简而言之,我要做的是:

  1. 选择“添加新的学生记录”选项。

  2. 当我尝试显示新添加的值(名字,姓氏,课程)时,它们全部返回为null。

我无法解决问题,但是我将尽力发布所需的代码,以使所有人都获得更好的视野。

主班:

public static void main(String[] args) throws Exception{


    String cont1=" ";
    String X;        
    int x1,arr_count=1;
    String[] option = {"First Name","Last Name","Course"};

    BufferedReader reader1=new BufferedReader(new InputStreamReader(System.in));
    do{      

    System.out.println("\nWelcome! Please select a task: \nA. View a record.\nB. Add a record.\nC. Edit a record.\nD. Exit the program.");
    String option1 = reader1.readLine();

    if("a".equals(option1)|| "A".equals(option1)){
        System.out.println("Please enter the Student ID for the record to view.");
        String a = reader1.readLine();
        x1 = Integer.parseInt(a);
        for(int y=0;y<3;y++){
        System.out.print(Records.getRecord(x1,y) + " ");
        }            
        System.out.print("\nSubjects and Grades: \n");
        for(int y=3;y<6;y++){
        System.out.print(Records.getRecord(x1,y) + " (" + Records.getRecord(x1,y+3) + ")\n");
        } 

        System.out.println("\nReturn to Main Menu? (Y/N)");
        cont1= reader1.readLine(); 
    }

    else if("b".equals(option1)||"B".equals(option1)){
        arr_count++;
        for (int y = 0; y<3;y++){
        System.out.println("Enter Value for "+ option [y]+":"  ); 
        X = reader1.readLine();
        X = Records.getRecord(arr_count,y);          
        }

        System.out.println("\nNew Student added:\nID number: "+ arr_count);
        for(int y=0;y<3;y++){
        X = Records.getRecord(arr_count,y);
        System.out.print(X + " ");
        }

记录类:

public class Records {

    public static String getRecord(int recordID,int index1)throws Exception{
        //BufferedReader reader1=new BufferedReader(new InputStreamReader(System.in));
        String records[][] = new String [10][9];
        records[0][0] = "John";    records[0][3] = "English";  records[0][6] = "C";
        records[0][1] = "Smith";   records[0][4] = "Math";     records[0][7] = "B";
        records[0][2] = "BS IT";   records[0][5] = "Graphics"; records[0][8] = "A";

        records[1][0] = "Juan";    records[1][3] = "Graphics"; records[1][6] = "B";
        records[1][1] = "Ponce";   records[1][4] = "Math";     records[1][7] = "B";
        records[1][2] = "BS ECE";  records[1][5] = "Robotics"; records[1][8] = "A";

        /**records[2][0] = "";     records[2][3] = "";     records[2][6] = "";
        records[2][1] = "";     records[2][4] = "";     records[2][7] = "";
        records[2][2] = "";     records[2][5] = "";     records[2][8] = "";

        records[3][0] = "";     records[3][3] = "";     records[3][6] = "";
        records[3][1] = "";     records[3][4] = "";     records[3][7] = "";
        records[3][2] = "";    records[3][5] = "";     records[3][8] = "";

        records[4][0] = "";     records[4][3] = "";     records[4][6] = "";
        records[4][1] = "";     records[4][4] = "";     records[4][7] = "";
        records[4][2] = "";    records[4][5] = "";     records[4][8] = "";*/



    return records[recordID][index1];
    }
}

我试图为records数组设置空白值,还将将为数组分配新值的函数放入Records类中,但是我仍然找不到方法来实现这一目的。

您永远不会设置记录的值。

X = reader1.readLine();
X = Records.getRecord(arr_count,y);  

正在丢弃读取的值。 在您的Record类中创建setValue方法-

X = reader1.readLine();
Records.setValue(arr_count, y, X);

--- inRecord setValue(x,y,val){records [x] [y] = val; }

看来您的主修课尚未完成。 do while循环停止的条件是什么? 除此循环外,main方法中还有其他内容吗?

暂无
暂无

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

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