3 回答
TA贡献1825条经验 获得超4个赞
您可以将输入参数存储到ArrayList
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
int n = 0;
int i = 1;
List<Integer> params = new ArrayList<>();
while (i <= 5) {
System.out.println("enter a number");
n = ns.nextInt();
params.add(n);
i++;
}
for (Integer param : params) {
System.out.println(param);
}
}
输出:
1
enter a number
2
enter a number
3
enter a number
4
enter a number
5
1
2
3
4
5
TA贡献1895条经验 获得超3个赞
请循环打印数字。请参阅下面的代码:
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
System.out.println(n);
i++;
}
TA贡献1856条经验 获得超5个赞
所有输入都被打印,问题是 while 循环将 n 设置为值 5,然后打印 5 五次。您正在寻找的正确代码是:
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
System.out.println(+n);
}
添加回答
举报