-
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!
-
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.
-
looks like the mod misedited my post, needs to fix the code tags ;/ i sent you a pm anyway.
Similar Threads
-
By ramchanderv in forum VB Classic
Replies: 0
Last Post: 04-27-2009, 02:38 AM
-
By yasinirshad in forum Database
Replies: 0
Last Post: 09-02-2007, 06:52 AM
-
Replies: 1
Last Post: 11-07-2006, 10:51 PM
-
By Alex Nitulescu in forum VB Classic
Replies: 4
Last Post: 04-27-2001, 06:11 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks