Results 1 to 6 of 6
- 02-21-2014, 10:28 PM #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:**
Create a program that asks for results of exams and calculates the mean average of the grades. Grades are floating point numbers between 4 and 10. Program asks for grades until a negative number is typed. If user gives a grade other than a number between 4 and 10, the text “Invalid grade!” will be printed on screen and program asks for another grade. Finally the program prints the number of inputted grades and their mean average on screen as shown in the example print. If no grades were inputted, the notice “You did not input any grades.” is the only thing printed on screen.
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.
Input a grade (4-10): 5
Input a grade (4-10): 6,5
Input a grade (4-10): 7,5
Input a grade (4-10): 7
Input a grade (4-10): 0
Invalid grade!
Input a grade (4-10): -4
4 grades inputted.
Average of the grades: 6.5
**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; Scanner reader = new Scanner(System.in); System.out.println("Program calculates the average of inputted grades."); System.out.println("Finish with a negative integer."); System.out.print("Input a grade (4-10): "); inputNumber = reader.nextInt(); while (inputNumber > 0 && inputNumber>=4 && inputNumber<=10) { sum += inputNumber; count++; System.out.print("Input a grade (4-10): "); inputNumber = reader.nextInt(); } if (inputNumber<4 || inputNumber>10) { System.out.println("Invalid grade!"); } if (count==0) { System.out.print("You didn't enter any data!"); } else { average = ((double)sum) / count; System.out.println(); System.out.println(count + " grades inputted."); System.out.print("Average of the grades: "+ average); } } }
Last edited by danielki; 02-22-2014 at 03:27 AM.
- 02-21-2014, 11:21 PM #2
Re: java Calculating the mean average
Please edit your post and wrap your code with code tags:
[code]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.If you don't understand my response, don't ignore it, ask a question.
- 02-21-2014, 11:40 PM #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?
- 02-21-2014, 11:54 PM #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.
If you don't understand my response, don't ignore it, ask a question.
- 02-22-2014, 03:28 AM #5
Member
- Join Date
- Feb 2014
- Posts
- 16
- Rep Power
- 0
Re: java Calculating the mean average
thanks, I think I edited it correctly.
- 02-22-2014, 03:39 AM #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.If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Calculating in Java app
By jeffrey159 in forum New To JavaReplies: 2Last Post: 09-11-2013, 08:43 AM -
Calculating Average using Arrays....help!
By knightwriter in forum New To JavaReplies: 5Last Post: 12-06-2011, 10:02 PM -
need some help finding average on java
By jtw0812 in forum New To JavaReplies: 2Last Post: 09-29-2010, 05:26 AM -
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04-06-2010, 06:03 AM -
Need help in calculating average value on this format ....
By motress in forum New To JavaReplies: 1Last Post: 03-11-2010, 05:21 AM
Bookmarks