英文:
How can i set an array length by user
问题
我正在尝试通过用户输入在Java中设置一个数组长度,并将一系列名称放入数组中。
问题出在开始“for”循环时。
似乎程序无法进入这个循环。
我在这里贴出了我遇到问题的代码。
请帮我解决这个问题
英文:
i'm trying to set by user input an array length in java and put inside the array a list of Names.
The problem is set when it's start the "for" cycle.
It's seems that the programm can't go inside this cycle.
I'm posting the code where I have got trouble.
Please help me how to solve this problem
int numPlayer = 0;
String [] listaNomi = new String []{};
numPlayer = myInput.nextInt();
for (int i=0; i<numPlayer; i++)
{
byte num=1;
System.out.println("Inserisci il nome del " +num);
System.out.print("Giocatore");
num++;
Nome = myInput.nextLine();
Nome = listaNomi[i];
}
Thanks
答案1
得分: 1
尝试将"byte num = 1"放在for循环外部。通过将其放在循环内部,每次循环运行时都会重新定义变量。
英文:
Try putting "byte num = 1" outside of the for loop. By putting it inside the loop each time the loop runs, the variable is redefined.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论