我一直在编写一个简单的 Java 代码,您可以通过控制台向程序提供 5 个数字,然后程序会告诉您选择了哪些数字并给出平均值。然而,我注意到,在我开始尝试测试我的代码后,for 循环基本上“跳过”了循环内的所有代码,我不知道为什么。这是我的代码:
import java.util.Scanner;
public class numAv {
public static void main(String[] args) {
int num;
int[] numbers = new int[5];
boolean done = false;
Scanner scan = new Scanner(System.in);
System.out.println("Enter five integer numbers one at a time.");
for (int i = 0; i >= 5; i++) {
scan.nextLine();
num = scan.nextInt();
numbers[i] = num;
}
// The code inside the for loop is being skipped; I'm not getting any time to type in an integer.
System.out.println("Your numbers are:");
for (int i = 0; i >= 5; i++) {
System.out.print(numbers[i]);
}
// The same has also happened above; The code within the for loop is essentially being skipped.
num = 0;
for (int i = 0; i >= 5; i++) {
num += numbers[i];
}
num /= (float) 5;
System.out.println("The average of the chosen numbers is " + num);
}
}
这是控制台输出的内容:
Enter five integer numbers one at a time.
Your numbers are:
The average of the chosen numbers is: 0
郎朗坤
函数式编程
相关分类