我一直在編寫一個簡單的 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
添加回答
舉報
0/150
提交
取消