View Single Post
  #3 (permalink)  
Old 04-23-2008, 06:38 AM
Eranga's Avatar
Eranga Eranga is offline
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,576
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Not only that root. If you take that line of code outside of the while loop the correct one should be,

Code:
average = (sum + 1)/(counter - 1) ;
because he update the counter also inside the while loop and for the input -1 also counter updates by one.

Here is much better way, which I really loved to used in my programming.

Code:
import java.util.*; public class ex { public static void main(String[] args) { double sum = 0; int counter = 0; double input = 0; do { System.out.print("ENTER A GRADE (OR ENTER -1 TO EXIT) "); input = new Scanner(System.in).nextDouble(); if(input != -1) { sum += input; counter++; } else { break; } }while (true); System.out.println("\nThe average is " + (sum/counter) ); } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote