# Beginner Loops for averages

• 10-17-2010, 05:22 PM
mobosecomin
Beginner Loops for averages
hi everyone :)

Im new to java just been using it a few weeks. I currently have an assignment that im working on and im having a few problems getting it to work.I hope someone can help.

what im trying to do is take input of exam results and and calculate an average when the user decideds by entering 0.

Im not sure how i get my program to recognize how many results have been entered and divide by that amount. I have made a program that can take a set number of amounts and get average but i want to increment a counter in this program.

Code:

```import java.io.*; public class examLoop{ public static void main (String[]args)throws IOException { double result=1; String grade; double average=0; int i=0; int sum=0; BufferedReader kbd=new BufferedReader(new InputStreamReader(System.in));         while ((result <=100)&&(result!0))         {         System.out.println("Please enter your exam result and enter 0 when you hace finished.");         result=Double.parseDouble(kbd.readLine());         if ((result>=80)&&(result<=100))         {                 grade = "honour";         System.out.println("Congratulations you recieved a "+grade);         }         if ((result>=40)&&(result<80))         {                 grade = "pass";         System.out.println("Congratulations you recieved a "+grade);             }         if ((result!=0)&&(result<40))         {                 grade = "failed";         System.out.println("I'm sorry you have "+grade);         }         sum+=result;         i++;         }         average =sum/i;         System.out.println("The average of the exam scores "+average); } //end of main method } // end of class```

***i have also tried version like this***

Code:

```import java.io.*; public class examLoop3{ public static void main (String[]args)throws IOException { double result=1; String grade; double average=0; int averageCount=0; double sum=0; BufferedReader kbd=new BufferedReader(new InputStreamReader(System.in));  System.out.println("Please enter your exam result and enter 0 when you hace finished.");     result=Double.parseDouble(kbd.readLine());     sum = result;           while ((result <=100)&&(result>0))       {       if ((result>=80)&&(result<=100))       {       grade = "honour";       System.out.println("Congratulations you recieved a "+grade);       }       if ((result>=40)&&(result<80))       {       grade = "pass";       System.out.println("Congratulations you recieved a "+grade);       }       if ((result!=0)&&(result<40))       {     grade = "failed";     System.out.println("I'm sorry you have "+grade);     }     sum+result;     if (result>0)     {     averageCount++;     }     }  average =sum/averageCount;  System.out.println("The average of the exam scores "+average); } //end of main method } // end of class```
Iv only been using loops for a few easy programs and im finding it tricky to find out whats the most efficient way to use them in this context .

Would really appreciate some help.

Moderator Edit: Code Tags Added
• 10-17-2010, 05:24 PM
mobosecomin
also im not sure how to make the code box that some users have on their threads so if someone could tell me i would as my code dose not look easy to read in that format. (not that it would make much sense anyway) :)
• 10-17-2010, 05:25 PM
Fubarable

Also, you'll want to fix your code indentation style so that it is uniform as this will also make your code easier to read, and the easier it is the to read, the more who will read it and help you.

Best of luck and welcome to the forum!
• 10-17-2010, 07:10 PM
mobosecomin
Ok so iv persevered for the past few hours and iv got this soloution. I feel for some reason that i am doing this the long way. Im manipulating some of my variables to start the loop and then i am having to subtract my counter by 1 at the end because i am using 0 as the trigger to exit the loop.

Im not sure if this can be done better and its really bothering me so if someone could take a look i would really appreciate it.Maybe its the only way of doing this at my beginner level im not sure :) Anyway this java is turning out to be quite challenging but great fun ;)

Code:

``` import java.io.*; public class examLoop{ public static void main (String[]args)throws IOException { double result=1; String grade; double average=0; int i=0; int sum=0; BufferedReader kbd=new BufferedReader(new InputStreamReader(System.in));  System.out.println("Please enter your exam results.");  System.out.println("When you have finished enter 0 to calculate.");   while ((result <=100)&&(result>0))   {     result=Double.parseDouble(kbd.readLine());   if ((result>=80)&&(result<=100))   {     grade = "honour";   System.out.println("Congratulations you recieved a "+grade);   }   if ((result>=40)&&(result<80))   {     grade = "pass";   System.out.println("Congratulations you recieved a "+grade);       }       if ((result!=0)&&(result<40))     {     grade = "failed";   System.out.println("I'm sorry you have "+grade);   }  sum+=result;  i++;   System.out.println("");   System.out.println("And your next result.  (Or 0 to calculate)"); }   average =sum/(i-1);   System.out.println("The average of the exam scores "+average); } //end of main method } // end of class```
• 10-17-2010, 07:28 PM
Fubarable
You really must take care to indent your code correctly and consistently else no one here will want to read it, and your instructor will likely take of marks. For instance, isn't this easier to read?

Code:

```import java.io.*; public class ExamLoop{   public static void main (String[]args)throws IOException {       double result=1;       String grade;       double average=0;       int i=0;       int sum=0;       BufferedReader kbd=new BufferedReader(new InputStreamReader(System.in));       while ((result <= 100) && (result > 0))  {         System.out.println("Please enter your exam result and enter 0 when you hace finished.");         result=Double.parseDouble(kbd.readLine());         if ((resul t>= 80) && (result <= 100)) {             grade = "honour";             System.out.println("Congratulations you recieved a "+grade);         }         if ((result >= 40) && (result < 80)) {             grade = "pass";             System.out.println("Congratulations you recieved a "+grade);         }         if ((result != 0) && (result < 40)) {             grade = "failed";             System.out.println("I'm sorry you have "+grade);         }         sum+=result;         i++;       }       average =sum/(i-1);       System.out.println("The average of the exam scores "+average);   } }```
One problem I see is that you're doing int division -- dividing an int by an int, and that always results in an int, not a double. To fix this, cast one of the numbers to a double:
Code:

`average = (double)sum / (i - 1);`
You also aren't checking for divide by zero errors or non-numeric entry, but your teacher may not require this of you at this time.
• 10-17-2010, 08:32 PM
mobosecomin
Yeah for sure ! thats allot easier to read actually im not sure what the standard indentation actually is so i was just guessing what i should be doing but il try to keep to a format similar to the way you have shown me and i think il go have a look at some other code for reference.

Ah yeah dividing an int by an int !! light bulb going off now had some problems with simple calculations in previous programs and i think it was down to this.
Anyway thanks for the advice Fubarable im starting my next problem now so I imagine I will be back here quite soon.

p.s i like the fractal avatar :)
• 10-17-2010, 09:17 PM
Fubarable
Quote:

Originally Posted by mobosecomin
Anyway thanks for the advice Fubarable im starting my next problem now so I imagine I will be back here quite soon.

You're welcome, and we look forward to seeing you back soon.

Quote:

p.s i like the fractal avatar :)
Ah yes, the Mandelbrot set, first described by Benoit Mandelbrot who passed away just yesterday -- may he rest in peace.