cow bears101 :
I've been working on a simple Java code where you are to give 5 numbers to the program through the console, which will then tell you what numbers you chose and give you an average. I noticed, however, after I started trying to test my code, that the for loop was basically 'skipping over' all of the code inside of the loop, and I have no idea why. Here's my code:
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);
}
}
Here's what the console outputs:
Enter five integer numbers one at a time.
Your numbers are:
The average of the chosen numbers is: 0
Evgeni Enchev :
Bad condition:
for (int i = 0; i >= 5; i++) {
This will never works, try this:
for (int i = 0; i < 5; i++) {
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=310456&siteId=1