3 回答

TA貢獻1825條經(jīng)驗 獲得超4個贊
您可以將輸入?yún)?shù)存儲到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條經(jīng)驗 獲得超3個贊
請循環(huán)打印數(shù)字。請參閱下面的代碼:
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條經(jīng)驗 獲得超5個贊
所有輸入都被打印,問題是 while 循環(huán)將 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);
}
添加回答
舉報