Preventing it from showing average


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Preventing it from showing average

  1. #1
    Join Date
    Mar 2011
    Posts
    1

    Preventing it from showing average

    Hello there! This is my first post on these forums and I hope I can get some good help, advice and hopefully start a few friendships here! I've recently been extremely interested in Java and I wish to learn more about it, simply because I need a hobby and this interests me.

    I've been going through a series of tutorials on YouTube by an extremely helpful user called 'thenewboston'. One of the things I've learned from his tutorials is how to make an 'averaging' program, in Eclipse. Well, I've made it just fine but I plan to expand it a little bit!

    This is the current code I have:
    Code:
    import java.util.Scanner;
    
    public class apples {
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		
    		double total = 0;
    		double grade = 0;
    		double average;
    		double counter = 0;
    		
    		while (counter < 10){
    			grade = input.nextDouble();
    			total = total + grade;
    			counter++;
    			
    			if (grade <=2){
    				System.out.println("Number is too low to be included. Try again.");
    				counter--;
    			}
    			average = total/10;
    			System.out.println("Your average is: "+ average);
    			
    		
    					
    			}
    		}
    		
    	}
    The only problem is when I run the program, after entering a grade the average is shown straight away... I do not want it to do this... So, my question is, how can I make it so it shows my average AFTER entering ten grades? If I rewrite the code, without the
    Code:
    if (grade <=2){
    				System.out.println("Number is too low to be included. Try again.");
    				counter--;
    			}
    , it runs fine.

    Thanks alot, I hope I can get some good responses!

  2. #2
    Join Date
    Mar 2011
    Posts
    4
    Noticed this forum whilst looking at this website. Had to register to answer your question though this place seems pretty empty ;p

    Your System.print line is inside your while loop, meaning that every time it loops its going to print that line, to make it print all at once simply put it outside the loop. as so:
    Code:
    		while (counter < 10){
    			grade = input.nextDouble();
    			total = total + grade;
    			counter++;
    
    			if (grade <=2){
    				System.out.println("Number is too low to be included. Try again.");
    				counter--;
    			}
    
    			}
            			average = total/10;
    			System.out.println("Your average is: "+ average);

    Bucky(thenewboston) is a great source to learn from, but should not be your only source, seems you either made a easy mistake (which happens to us all when learning) or your not really clear on your understanding of iterations. hope it helped you anyway.
    Last edited by Hack; 03-31-2011 at 07:51 AM.

  3. #3
    Join Date
    Mar 2011
    Posts
    4
    looks like the mod misedited my post, needs to fix the code tags ;/ i sent you a pm anyway.

Similar Threads

  1. Not showing all pages data
    By ramchanderv in forum VB Classic
    Replies: 0
    Last Post: 04-27-2009, 02:38 AM
  2. How do I Calculate Average Leadtime in sqlserver...
    By yasinirshad in forum Database
    Replies: 0
    Last Post: 09-02-2007, 06:52 AM
  3. Get Average of Array - Visual Basic 2003
    By wartech in forum .NET
    Replies: 1
    Last Post: 11-07-2006, 10:51 PM
  4. Prevent showing the Custom Colors
    By Alex Nitulescu in forum VB Classic
    Replies: 4
    Last Post: 04-27-2001, 06:11 PM
  5. Prevent showing the Custom Colors
    By Alex Nitulescu in forum VB Classic
    Replies: 0
    Last Post: 04-27-2001, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center