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 3 of 3

Thread: Exercise 5.1 in (intro to javaprogramming 10th edition)

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Exercise 5.1 in (intro to javaprogramming 10th edition)

    /*
        (Count positive and negative numbers and compute the average of numbers) Write
        a program that reads an unspecified number of integers, determines how many
        positive and negative values have been read, and computes the total and average of
        the input values (not counting zeros). Your program ends with the input 0. Display
        the average as a floating-point number. Here is a sample run:
     
          Enter an integer, the input ends if it is 0: 1 2 -1 3 0
          The number of positives is 3
          The number of negatives is 1
          The total is 5.0
          The average is 1.25
    */
     
    //So this was my answer but when the program starts running, the compiler freezes.
    //Tried using intelliJ, same result but allowed me to enter numbers without going any further.
     
    import java.util.*;
     
    public class testing7 {
      public static void main(String[] args) {
     
        Scanner input = new Scanner(System.in);
     
        System.out.println("Enter an integer (The input ends if it is 0): ");
        int integer = input.nextInt();
     
        int count = 0;
        int negativeCount = 0;
        int positiveCount = 0;
        double total = 0;
     
        while(integer != 0) {
            if(integer < 0)
              negativeCount++;
             else
              positiveCount++;
     
         total += integer;
         count++;
        }
     
        System.out.println("The number of positives is: " + positiveCount);
        System.out.println("The number of negatives is: " + negativeCount);
        System.out.println("The total is: " + total);
     
        double average = total / count;
     
        System.out.println("The average is:  " +  average);
     
      }
    }

    Hints only if possible please :
    Last edited by UniverseCloud; January 22nd, 2019 at 02:49 PM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Exercise 5.1 in (intro to javaprogramming 10th edition)

    What do you think this loop is doing? Go thru it carefully by hand.

    while(integer != 0) {
            if(integer < 0)
              negativeCount++;
             else
              positiveCount++;
     
         total += integer;
         count++;
    }

    Regards,
    Jim

  3. The Following User Says Thank You to jim829 For This Useful Post:

    UniverseCloud (January 22nd, 2019)

  4. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 5.1 in (intro to javaprogramming 10th edition)

    I thought long and hard at the time, i kept going through the other questions but i couldn't continue and got frustrated to so i went to redo chapter 3/4/5 and back to the question and noticed that i didn't even place the question again for the user to input TT.

Similar Threads

  1. Java Micro Edition (ME) Help.
    By Samystick in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: January 6th, 2018, 04:14 PM
  2. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  3. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  4. ISBN-10 enter 9 digit to find the 10th digit
    By cldance5678 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2013, 02:52 PM
  5. ManilaJam - JavaProgramming Newbie
    By manilajam in forum Member Introductions
    Replies: 2
    Last Post: October 27th, 2012, 10:47 PM