# Thread: java Calculating the mean average

1. Member Join Date
Feb 2014
Posts
16
Rep Power
0

## java Calculating the mean average

I am trying to do the following java assignment and every thing seems to work fine except when I put a number<4 or >10 it prints both "Invalid grade!"
"You didn't enter any data!" what I wanted is to print only "Invalid grade!" I tried to fix it but I couldn't. Can any one help me please?

Thanks,

**Assignment:**

Hint:A double type variable is to be used to store the value of the average.

Program is written to a class called Average.

Example output

Program calculates the average of inputted grades.
Finish with a negative integer.

**Code**
Java Code:
``` import java.util.Scanner;
public class apples {

public static void main(String[] args) {

int inputNumber=0;
int sum;
int count;
double average;

sum = 0;
count = 0;

System.out.println("Program calculates the average of inputted grades.");
System.out.println("Finish with a negative integer.");

while (inputNumber > 0 && inputNumber>=4 && inputNumber<=10) {
sum += inputNumber;
count++;

}

if (inputNumber<4 || inputNumber>10) {
}

if (count==0) {
System.out.print("You didn't enter any data!");
}
else {
average = ((double)sum) / count;
System.out.println();
System.out.print("Average of the grades: "+ average);
}

}

}```
Last edited by danielki; 02-22-2014 at 02:27 AM.  Reply With Quote

2. ## Re: java Calculating the mean average

[code]
[/code]
to get highlighting and preserve formatting.  Reply With Quote

3. Member Join Date
Feb 2014
Posts
16
Rep Power
0

## Re: java Calculating the mean average

Hello Norm,
How do I edit my post and wrap with code tags? I am new to the forum.
Is there a link that I have to follow?  Reply With Quote

4. ## Re: java Calculating the mean average

Can you see an "Edit Post" in shaded bar below and to the right of your post? Click that.  Reply With Quote

5. Member Join Date
Feb 2014
Posts
16
Rep Power
0

## Re: java Calculating the mean average

thanks, I think I edited it correctly.  Reply With Quote

6. ## Re: java Calculating the mean average

That looks better.

Normally the tests for valid data are done inside the loop. Execution does not exit the loop until the user has entered valid data or asked to quit the program. This code checks the data outside of the loop.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•