简体   繁体   English

从方法中包含的数组中打印特定元素

[英]Printing specific element from an array contained in a method

Trying to call one element from my array that is in a method. 尝试从方法中的数组中调用一个元素。 However, when I call and display the element, I get a compiling error. 但是,当我调用并显示该元素时,出现编译错误。 I don't know how to properly print out an individual level. 我不知道如何正确打印个人水平。

sortMarks is an arrayList, and has already been initialized. sortMarks是一个arrayList,并且已经被初始化。

The code is supposed to display how many marks that the user inputted in a specific level. 该代码应该显示用户在特定级别输入的标记数。 This is all done in a GUI. 所有这些都在GUI中完成。

Here's my code: 这是我的代码:

private int[] arrayToLevel() {
    //Creating array for the different levels
    int levels[] = {0, 0, 0, 0, 0};

    //Finding how many marks at a certain level using the levels array
    int grade;
    for(int i=0; i<sortMarks.size(); i++) {

    grade = sortMarks.get(i);

    if (grade < 50) {
        levels[0] = levels[0]+1;           
    }
    else if(grade >= 50 && grade < 60){
        levels[1] = levels[1]+1;            
    } 
    else if (grade >= 60 && grade < 70){
        levels[2] = levels[2]+1;            
    } 
    else if (grade >= 70 && grade < 80){
        levels[3] = levels[3]+1;            
    } 
    else if (grade >= 80){
        levels[4] = levels[4]+1;            
    }        

    }

    return levels;
}

Here is where my errors begins, I don't know how to properly print this out. 这是我的错误开始的地方,我不知道如何正确打印出来。

analyseMarks.append("\nStudents at Level R: " + arrayToLevel.get[0].toString());

analyseMarks.append("\nStudents at Level 1: " + arrayToLevel.get[1].toString());

    //and so on for the rest of the levels.

EDIT: 编辑:

Just to let every know for future reference my compilation error was 只是为了让大家知道以供将来参考,我的编译错误是

cannot find symbol
symbol: variable arrayToLevel

It is not completely clear without the compilation error, but I believe the problem is with the attempted use of .get on an int[] . 没有编译错误还不是很清楚,但是我相信问题在于尝试在int[]上使用.get

Changing to be: 更改为:

analyzeMarks.append(... + Integer.toString(arrayToLevel()[0]));

should resolve the compilation error. 应该解决编译错误。

The correct way to use the result of your method is : 使用方法结果的正确方法是:

arrayToLevel()[0]

Instead of this : 代替这个:

arrayToLevel.get[0].toString()

You don't even need to use toString() , because it will be concatinated with a String 您甚至不需要使用toString() ,因为它将以String表示。

Also Instead of : 也代替:

analyseMarks.append("\nStudents at Level R: " + arrayToLevel());

the practice way to append to StringBuilder is : 附加到StringBuilder的实践方法是:

analyseMarks.append("\nStudents at Level R: ");
analyseMarks.append(arrayToLevel()[0]);

Or : 要么 :

analyseMarks.append("\nStudents at Level R: ").append(arrayToLevel()[0]);

You only need to call the method once. 您只需要调用一次该方法。 Use a reference to the return value in your print statements. 在打印语句中使用对返回值的引用。

int[] levels = arrayToLevel();

analyseMarks.append("\nStudents at Level R: " + levels[0]); 
analyseMarks.append("\nStudents at Level 1: " + levels[1]);

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

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