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: Average Won't Work

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Average Won't Work

    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class Average {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		int value, count = 1, sum = 0;
     		double average;
     		System.out.println("Enter an int (0 to quit): ");
     		value = scan.nextInt();
     
     		while (value != 0)
     		{
      			count+= 1;
     			sum += value;
     			System.out.println("The sum so far is " + sum);
     			System.out.print("Enter an integer (0 to quit): ");
     			value = scan.nextInt();
     			average = ((double)sum) / count;
     			DecimalFormat fmt = new DecimalFormat("0.###");
     				System.out.println("Average: " + fmt.format(average));
     		 		System.out.println("Num of values entered: " + count);
     				}
     		if (value == 0){
     			System.out.println("No Average; program was terminated");
     			System.exit(value);
     	}
     		}
    	}
    Last edited by Norm; October 22nd, 2013 at 09:58 AM. Reason: Changed \ to / in end code tag


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Average Won't Work

    Do you have a question?

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Average Won't Work

    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class Average {
       public static void main (String[] args) {
          int sum = 0, value, count = 0;
          double average;
          Scanner scan = new Scanner (System.in);
          System.out.print ("Enter an integer (0 to quit): ");
          value = scan.nextInt();
          while (value != 0)  // sentinel value of 0 to terminate loop
          {
             count++; 
             sum += value;
             System.out.println ("The sum so far is " + sum);
             System.out.print ("Enter an integer (0 to quit): ");
             value = scan.nextInt();
          }
          System.out.println ();
          System.out.println ("Number of values entered: " + count);
          average = (double)sum / count;
          DecimalFormat fmt = new DecimalFormat ("0.###");
          System.out.println ("The average is " + fmt.format(average));
       }
    }
    Found this from one of my projects, you could use it if you want.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Location
    Seattle, Washington
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Average Won't Work

    Why won' the average work? when i enter 10 for the first number, then 20 for the second number, it returns an average of 5.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Average Won't Work

    @D.K. Welcome to the forums. This forum's philosophy is to try and help posters with a question by helping them step through there logic and thought process, which can be hard without knowing a question (hence post #2). This is as opposed to saying - here's the answer (in fact doing the latter doesn't help in learning the important skill of problem solving, and in many cases would be against academic rules). This is more clearly discussed in the following article: http://www.javaprogrammingforums.com...n-feeding.html

    --- Update ---

    Quote Originally Posted by gotdatdough View Post
    Why won' the average work? when i enter 10 for the first number, then 20 for the second number, it returns an average of 5.
    Post your new code. Given the code fed to you above, we can only guess what the code you are using looks like (and please keep in mind my note to D.K. above)

  6. The Following User Says Thank You to copeg For This Useful Post:

    PhHein (October 23rd, 2013)

  7. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Average Won't Work

    Quote Originally Posted by copeg View Post
    @D.K. Welcome to the forums. This forum's philosophy is to try and help posters with a question by helping them step through there logic and thought process, which can be hard without knowing a question (hence post #2). This is as opposed to saying - here's the answer (in fact doing the latter doesn't help in learning the important skill of problem solving, and in many cases would be against academic rules). This is more clearly discussed in the following article: http://www.javaprogrammingforums.com...n-feeding.html

    --- Update ---



    Post your new code. Given the code fed to you above, we can only guess what the code you are using looks like (and please keep in mind my note to D.K. above)
    Alright.

Similar Threads

  1. Image won't work!
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:08 PM
  2. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  3. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 09:44 PM
  4. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM