gpt4 book ai didi

Java 初学者作业

转载 作者:行者123 更新时间:2023-12-04 21:29:40 25 4
gpt4 key购买 nike

我是 Java 的新手,我正在尝试编写一个程序让用户输入一定数量的整数,然后检索这些整数并将它们打印回来。

这仅在您输入 1 个整数时有效。否则,它终止。

import java.util.Scanner;
public class Assignment4 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.println("Enter the Number of Numbers");
int totalNumbers = scan.nextInt();
int[] numbers = new int[totalNumbers];
for(int i = 0;i == (totalNumbers-1); i++){
System.out.println("Enter the Next Number");
numbers[i] = scan.nextInt();
}


for(int i = 0;i == (totalNumbers-1); i++){
System.out.println(numbers[i]);
}

}
}

最佳答案

你的循环条件是你的问题。它只会接受一个输入。条件:

for(int i = 0; i == (totalNumbers-1); i++){

将在 i != (totalNumbers - 1) 时终止。您的 for 循环应该像下面这样阅读:

Initialize a new variable i to zero, then while i is equal to "totalNumbers - 1", do the following block of code, then increment i by 1

你可能想要:

for(int i = 0; i <= (totalNumbers-1); i++){

可以理解为:

Initialize a new variable i to zero, then while i is LESS THAN or equal to "totalNumbers - 1", do the following block of code, then increment i by 1

关于Java 初学者作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151539/

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