如何根据用户输入创建数组?

时间:2014-11-15 14:12:08

标签: arrays

我有以下代码:

public class PlayerName {

     public void playerName(){

        int []playerNames = new int[playerNames[[n]];
        for(int n=0; n<=playerNames[n];n++){
            playerNames[n] = Helper.readInt("Enter no of players playing:  " );

        }
    }

}

我一直在尝试根据用户输入初始化一个数组,但他们说我必须创建一个名为n的局部变量。

1 个答案:

答案 0 :(得分:1)

你必须先设置它才能像这样

public class PlayerName {
    public void playerName(){
    int x = Helper.readInt("Enter the dynamic For Value:  ");
    int []playerNames = new int[playerNames[[x]];
        for(int n=0; n<x;n++){
            playerNames[n] = Helper.readInt("Enter no of players playing:  " );

        }
    }
}