planker1010
planker1010

Reputation: 75

Passing array through average method, resulting in low percent

For some reason the average is being populated wrong when I pass the array to the method I get a really low percent. It almost seems like since the Array shotsMade is only recording integers for made shots and not misses it is not calculating off the right base.

import java.util.*;

public class Test {
public static void main(String[] args) {
    int myGameCounter = 1;  
    int shotCount = 0;
    int shotCount1 = 0;
    int [] shotsMade = new int [5];
    int sum = 0;

    System.out.print("Enter Player's Free Throw Percentage: ");
    Scanner input = new Scanner(System.in);
    int percent = input.nextInt();


    //Game #1
    System.out.println("Game " + myGameCounter + ":");
    Random r = new Random();
    myGameCounter++;
    shotCount = 0;
    for (int i = 0; i < 10; ++i){
        boolean in = tryFreeThrow(percent);
        if (in) {
        shotCount++;
        System.out.print("In" + " ");
        }
        else {
        System.out.print("Out" + " ");
        }
    }

    System.out.println("");
    System.out.println("Free throws made: " + shotCount + " out of 10");
    shotsMade[0]= shotCount;

        //Game #2
    System.out.println("");
    System.out.println("Game" + myGameCounter + ":");
    myGameCounter++;
    shotCount1 = 0;
    for (int i = 0; i < 10; ++i){
        boolean in = tryFreeThrow(percent);
        if (in) {
        shotCount1++;
        System.out.print("In" + " ");
        }
        else {
        System.out.print("Out" + " ");
        }   
    }
    System.out.println("");
    System.out.println("Free throws made: " + shotCount1 + " out of 10");
    shotsMade[1]= shotCount1;

    System.out.println("");
    System.out.println("Summary:");



    System.out.println("Best game: " + max(shotsMade)); 
    System.out.println("Total Free Throws Made: " + sum(shotsMade) + " " + "out of 20");
    System.out.println("Average Free Throw Percentage: " + average(shotsMade) +"%");    

}//main

public static boolean tryFreeThrow(int percent) {
    Random r = new Random();
    int number = r.nextInt(100);
    if (number > percent){ 
     return false;
    }
    return true;
}

public static float average(int nums[]) {
    int total = 0;
    for (int i=0; i<nums.length; i++) {
        total = total + nums[i];
    }
    float f = (total / nums.length);
    return (float)total /(float)nums.length;
}

public static int sum(int nums[]) {
    int sum = 0;
    for (int i=0; i<nums.length; ++i) {
        sum += nums[i];
    }
    return (int)sum;
}

public static int max(int nums[]) {
    int max=nums[0];
    for (int i=1; i<nums.length; i++) {
        if (nums[i] > max) 
            max = nums[i];
    }
    return max;
}

}//class

Upvotes: 0

Views: 171

Answers (3)

NickJ
NickJ

Reputation: 9559

Among others, your expression

float f = (total / nums.length);

will yield an inaccurate result.

Both total and nums.length are integers, and any operation between integers always results in an integer.

Example: if total=10 and nums.length=3, you'd expect the result to be 3.333... but actually the result is just 3. Only after that do you cast it to a float, resulting in 3.0.

To get the required result, you need to cast both integers to floats before dividing:

float f = (float) total / (float) nums.length;

Upvotes: 0

user1793963
user1793963

Reputation: 1335

You are calculating the avarage of 5 numbers but you only set 2. So if all shots hit your array will look like this: 10, 10, 0, 0, 0 and the avarage will be 4.

Upvotes: 1

SJuan76
SJuan76

Reputation: 24885

Old issue, you are using integer arithmetic total / nums.length with returns you an int value. You later assign it to a float, but the value already has been truncated.

Just change one of the values to float before the division, v.g. ((float) total) / num

Upvotes: 1

Related Questions