My code is:
public class computeAverage {
public static void main(String[] args) {
int inputNumber; // one of the ingtegers input by the user
int sum; // the sum of the positive integers
int count; // the number of positive integers
double average; // the average of positive integers
sum = 0;
count = 0;
TextIO.put("Enter your first positive integer: ");
inputNumber = TextIO.getlnInt();
[B]while (inputNumber <0) {
System.out.println("\nError: Input a positive integer.");
TextIO.put("Enter your first positive integer: ");
}[/B]
while (inputNumber != 0) {
sum += inputNumber;
count++;
TextIO.put("Enter your next positive integer, or 0 to end: ");
inputNumber = TextIO.getlnInt();
}
if (count == 0) {
TextIO.putln("You didn't enter any data!");
} else {
average = ((double)sum) / count;
TextIO.putln();
TextIO.putln("You entered " + count + " positive integers.");
TextIO.putln("Their average is " + average + ".");
}
}
}
The bold part is where my problem is. If the user enters a number such as -2 the program is to stop and ask for another number but it continues to print the two lines to the console in the while loop.


Sign In
Create Account


Back to top









