2 Replies - 7302 Views - Last Post: 30 September 2010 - 02:37 PM Rate Topic: -----

#1 Grim-XTO   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

testing for averages in a while loop

Posted 30 September 2010 - 11:39 AM

Hi again folks,
Sorry for making two posts in such short amount of time but I really want to be "un-stumped" so to say on while loops and how to make them correct in what I need for a program. For this while loop, I am attempting to make it so that you can enter a bunch of tests and then figure out the average after you are done entering these tests. I also need to pick a sentinel which I had thought I did with the 999. This program doesn't really do anything for me. If I enter the 999 sentinel value it just seems to divide that by the number of entrances and not all of the inputs together divided by the number of inputs. Not really sure where I went wrong with this. I assume I might not have set up the sentinel value properly or some such thing. I am a little confused how to get the while loop to accept multiple inputs and then average them out. Since there is no set number of how many tests I will enter each time I run the program, I really can't just declare all of these tests as variables. Well, this one is messed up and I would appreciate the help thank you.
import java.util.*;
public class testLab2
{
  public static void main(String[] args)
  {
    int count;
    int testScore;
    int total;
    int average;
    count = 0;
    total = 0;
    
    
    Scanner input = new Scanner(System.in); //1
    System.out.println("Enter the Test Score"); //1
    testScore = input.nextInt(); //1
    
    while (testScore != 999) //2
    {
      if (testScore >= 90)
        System.out.println("A");
      else if (testScore >= 80)
        System.out.println("B");
      else if (testScore >= 70)
        System.out.println("C");
      else if (testScore >= 60)
        System.out.println("D");
      else
        System.out.println("F");
      
      count++; //3
      total = total + testScore; //3
      testScore = input.nextInt(); //3
      
    if (count > 0) 
    {
      average = testScore/count;
      System.out.println("Test Average is: " + average);
    }
  }
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: testing for averages in a while loop

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: testing for averages in a while loop

Posted 30 September 2010 - 12:09 PM

Don't forget to initialize count to 1, and add the input to the total variable before the loop as well. This will set the count and sum to the values you are expecting.
Was This Post Helpful? 0
  • +
  • -

#3 Grim-XTO   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 168
  • Joined: 20-September 10

Re: testing for averages in a while loop

Posted 30 September 2010 - 02:37 PM

Ok so in the initial declaring of variables and such I made count = 1. As for adding the input to the total before the loop I am not sure how to go about doing that. Total is more or less an accumulator correct?So how would I add the input to the total variable? total = testScore; ?
Wow and I just realized by running the program that now after I enter the first two numbers it creates an average out from then after every number I enter. Did I also mess up on the average calculations as well? I think I really boned this one =(

This post has been edited by Grim-XTO: 30 September 2010 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1