Results 1 to 3 of 3
  1. #1
    jehduncs is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    jehduncs is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Need help to find average
    By kevinsoto in forum New To Java
    Replies: 4
    Last Post: 11-04-2010, 01:54 PM
  2. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 09:52 PM
  3. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM
  4. Calculating average
    By clocksaysits9 in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 05:03 AM
  5. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM

Posting Permissions

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