1. Member
Join Date
Dec 2010
Posts
2
Rep Power
0

## Average returns 0[solved]

Im trying to get the average of number of marks divded by the total marks such as 3 marks of 10, 10 and 10 and have the total marks 50 so 30/50

Java Code:
```import java.util.Scanner;
public class MarksAverage
{

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("How many scores will be entered? ");
int numTests = input.nextInt();

int [] marks = new int [numTests];

for(int counter =0; counter < marks.length; counter++)
{
System.out.print("Enter mark # "+ (counter+1) + ":");
marks[counter] = input.nextInt();
}

int runningTotal = 0;

for(int counter = 0; counter < marks.length; counter++)
{
runningTotal += marks[counter];
//calculate average after the loop
}//end for

//calculate average

System.out.print("What is the worth of all marks");
int marksWorth = input.nextInt();

int average = (int) (runningTotal / marksWorth) * 100;

System.out.println(average);
}

}```
Last edited by jehduncs; 12-03-2010 at 06:30 PM.

2. You seem to be doing int division -- an int divided by an int -- which will always result in an int. So 3/6 will give 0, and 5/3 will give one. The solution is to use double by casting one of the ints to double:

Java Code:
`double average = (double) myIntA / myIntB;`

3. Member
Join Date
Dec 2010
Posts
2
Rep Power
0
thank you, works perfectly:)

#### Posting Permissions

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