Results 1 to 3 of 3
Thread: Average returns 0
 12032010, 05:29 PM #1Member
 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; 12032010 at 06:30 PM.

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;
 12032010, 06:29 PM #3Member
 Join Date
 Dec 2010
 Posts
 2
 Rep Power
 0
Similar Threads

Need help to find average
By kevinsoto in forum New To JavaReplies: 4Last Post: 11042010, 01:54 PM 
average
By anjigadu in forum New To JavaReplies: 4Last Post: 09192010, 09:52 PM 
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09122010, 11:59 PM 
Calculating average
By clocksaysits9 in forum New To JavaReplies: 4Last Post: 04062010, 05:03 AM 
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06132008, 11:09 AM
Bookmarks