gpt4 book ai didi

从收集循环接收到的 Java 打印输入

转载 作者:行者123 更新时间:2023-12-01 18:09:05 25 4
gpt4 key购买 nike

如何显示用户在此 while 循环中输入的所有输入?

Scanner input = new Scanner (System.in);

int times=5;
int i = 1;

while( i <= times){
System. out. print(" Enter Number : ");
int number = input.nextInt();
i++;
}

//Show all the input

最佳答案

试试这个::

public static void main(String[] args) {
Scanner input = new Scanner (System.in);

int times=5;
int i = 1;
List<Integer> mylist=new ArrayList<>();

while( i <= times){
System. out. print(" Enter Number : ");
mylist.add(input.nextInt());
i++;
}
System.out.println(mylist.toString());

}

输出::

 Enter Number : 1
Enter Number : 2
Enter Number : 3
Enter Number : 4
Enter Number : 5
[1, 2, 3, 4, 5]

关于从收集循环接收到的 Java 打印输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60499312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com