Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Something wrong with computing the average

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Something wrong with computing the average

    I have to compute the average in this program, but the average cannot consists of zero which ends the program. For example if I enter 1, 2, 3, -1, then 0 to exit, the average will be the sum (5) over 4 and not 5 since the last number, the zero does not go in the sum. Any help?

    Thanks.

    public class Problddm1 {
        public static void main (String []args){
            Scanner scan = new Scanner(System.in);
     
     // Object declaration
            int number = 1;
            int sum = 0;
            float average = 0;
            float count = 0;
            int posnum =0;
            int negnum =0;
     
    // While loop
            do{
                count++;
                System.out.print("Enter a number, program exits on 0: ");
                number = scan.nextInt();
                sum = sum + number;
                average = sum / count;
     
              if (number <0) {
                  negnum++;
              }
              else if (number >0) {
     
                    posnum++;
              }
            } while (number != 0);
     
    // Results
            System.out.println("The number of positives is: " +posnum);
            System.out.println("The number of negatives is: " +negnum);
            System.out.println("The total is: " + count);
            System.out.println("The average is: " +average);
     
        }
     
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Something wrong with computing the average

    A do-while loop will do something, and then check to see whether it should be repeated.

    A while loop will check if something should be done, and if so, do it and repeat the check.

    You want to do the latter. You're doing the former.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Something wrong with computing the average

    Still doesn't work

    I switch the do{ with while (number != 0){ and deleted the while in the do

    Stupid zero.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Something wrong with computing the average

    Quote Originally Posted by maximus20895 View Post
    Still doesn't work .
    "It doesn't work" is one of the least informative statements you can make. What exactly did you try (an SSCCE answers that question). What does it do? What did you expect it to do?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Something wrong with computing the average

    Haha, sorry.

    It does the same thing as the first which is when it calculates the average it adds the zero with it. So the average goes way down. I just want it to calculate any - or + numbers and not include the zero. It always includes the zero since you have to enter 0 in order to close the program.

    I changed the do-while loop to a while loop.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Something wrong with computing the average

    You aren't scanning the next number until after you've already entered the while loop, which is why it's not working. You have a check to see whether the number is positive or negative. Why don't you do something similar to check that it's not zero?

Similar Threads

  1. Finding the Average
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2010, 07:58 PM
  2. project on mobile computing
    By seejay in forum Java Theory & Questions
    Replies: 2
    Last Post: May 18th, 2010, 02:31 AM
  3. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM
  4. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM
  5. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM