# Thread: Beginner Loops for averages

1. Member
Join Date
Oct 2010
Posts
23
Rep Power
0

## 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.

Java 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***

Java 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
Last edited by mobosecomin; 10-17-2010 at 06:02 PM. Reason: Moderator Edit: Code Tags Added

2. Member
Join Date
Oct 2010
Posts
23
Rep Power
0
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) :)

3. I have added code tags to your post above to try to help your code retain its formatting and be readable. To learn to do this yourself, please read the first link in my signature below. Simplified you just place the start code tag [cod&#101;] above your code block and the end code tag [/cod&#101;] below your code block.

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!

4. Member
Join Date
Oct 2010
Posts
23
Rep Power
0
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 ;)

Java 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```
Last edited by mobosecomin; 10-17-2010 at 07:19 PM.

5. 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?

Java 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:
Java 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.

6. Member
Join Date
Oct 2010
Posts
23
Rep Power
0
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 :)

7. 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.

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.

#### Posting Permissions

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