Results 1 to 6 of 6
  1. #1
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default 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.

  3. #3
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default 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?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default 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.

  5. #5
    danielki is offline Member
    Join Date
    Feb 2014
    Posts
    16
    Rep Power
    0

    Default Re: java Calculating the mean average

    thanks, I think I edited it correctly.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default 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

  1. Calculating in Java app
    By jeffrey159 in forum New To Java
    Replies: 2
    Last Post: 09-11-2013, 08:43 AM
  2. Calculating Average using Arrays....help!
    By knightwriter in forum New To Java
    Replies: 5
    Last Post: 12-06-2011, 10:02 PM
  3. need some help finding average on java
    By jtw0812 in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 05:26 AM
  4. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 06:03 AM
  5. Replies: 1
    Last Post: 03-11-2010, 05:21 AM

Posting Permissions

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