Preventing it from showing average
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

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
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Preventing it from showing average

    Move the average calculation and display to after the 'while' loop.

    One must learn by doing the thing; for though you think you know it, you have no certainty, until you try...
    Sophocles
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center