如何让用户设置数组长度

huangapple go评论62阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月7日 00:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63766406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定