计算java程序中方法的最大字段目标百分比

时间:2014-02-18 02:59:24

标签: java

必须在球队投篮命中率的一组数据中找到最大值和最小值。

import java.io.*;
import java.util.Scanner;
public class kstatebball
{
    public static void main(String [] args)
    throws FileNotFoundException
    {
        Scanner in = new Scanner(new File("data1.txt"));
        String[] firstname = new String [100];
        String[] lastname = new String [100];
        int fgm[] = new int [100];
        int fga[] = new int [100];
        double fgp = 0;        
        int maxFGP;
        int minFGP;              

        System.out.print("         Player              FGM     FGA             FGP%\n");
        int  count = 0;
        for( int i = 0;in.hasNext(); i++)
        {
            firstname[i] = in.next(); 
            lastname[i] = in.next();
            fgm[i] = in.nextInt();
            fga[i] = in.nextInt();
            fgp = (1.0 * fgm[i]) / (1.0 * fga[i]) * 100;
            count++;           
            System.out.printf("%10s %10s        %3d     %3d             %3.1f  \n",firstname[i],lastname[i],fgm[i],fga[i],fgp);            
        }
        maxFGP = maxFGP(fgm,fga,count);
        minFGP = minFGP(fgm,fga,count);
        System.out.printf("\n\nThe player with the highest field goal percentage is: %3d ",maxFGP(fgm,fga,count));
        System.out.printf("\nThe player with the lowest field goal percentage is : %3.1f",fgp);
    }

    public static int maxFGP(int[] fgm, int[] fga, int count)
    {
        int max = 0;
        for(int i = 0; i < count; i++)
        {
            if((1.0 * fgm[i]) / (1.0 * fga[i]) * 100 > max) 
                max = i;            
        }
        return max;
    }



    public static int minFGP(int[]fgm, int[]fga, int count)
    {
        int min = 0;
        for(int i = 0; i > 13; i++)
        {
            if(fgm[i] > fgm[count])
                count = i;            
        }
        return min;
    }        
}

需要“if”语句的帮助才能返回正确的最大值

我们拥有所有球员的百分比,但需要使用这些方法才能找到具有最高投篮命中率和最低投篮命中率的球员。

这是我正在使用的数据文件:

Marcus Foster    123   288
Thomas Gipson    102   178
Shane Southwell  88    224
Will Spradling   58    144
Wesley Iwundu    53    111
Nino Williams    49    96
Nigel Johnson    28    80
Jevon Thomas     15    58
D.J. Johnson     34    68
Omari Lawrence   27    65
Shawn Meyer      2     4
Ryan Schultz     2     9
Jack Karapetyan  1     4
Brian Rohleder   1     2

3 个答案:

答案 0 :(得分:2)

应该是这样的:

public static int maxFGP(int[] fgm, int[] fga, int count)
{
    int max = 0;
    double maxValue=(1.0 * fgm[0]) / (1.0 * fga[0]) * 100;
    for(int i = 0; i < count; i++)
    {
        if((1.0 * fgm[i]) / (1.0 * fga[i]) * 100 > maxValue)                                                      
        {
            max = i;  
            maxValue=(1.0 * fgm[i]) / (1.0 * fga[i]) * 100;  
        }        
    }
    return max;
}

注意:您已设置int max = 0;,因此无需从i=0循环播放。改为int i=1而不是

以最大值打印播放器的名称

System.out.printf("\n\nThe player with the highest field goal percentage is: %3d ",firstname[maxFGP(fgm,fga,count)]);

答案 1 :(得分:0)

max是一个数组索引,我建议你添加另一种方法来计算百分比

public static int maxFGP(int[] fgm, int[] fga, int count)
{
    int max = 0;
    for(int i = 1; i < count; i++){
        if(fieldGoalPercentage(fgm, fga, i) > fieldGoalPercentage(fgm, fga, max)) 
            max = i;            
    }
    return max;
}

答案 2 :(得分:0)

Java是一种面向对象的语言。你可能是初学者,但这是考虑它的另一种方式。

从Player对象开始:

package misc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * misc.Player
 * @author Michael
 * @link  http://stackoverflow.com/questions/21843505/calculating-max-field-goal-percentage-in-a-method-in-java-program
 * @since 2/17/14 10:07 PM
 */
public class Player {

    private final String firstName;
    private final String lastName;
    private final int numAttempts;
    private final int numMakes;

    public static void main(String[] args) {
        List<Player> team = new ArrayList<Player>() {{
            add(new Player("Larry", "Bird", 40, 80));
            add(new Player("Robert", "Parish", 4, 10));
            add(new Player("Dennis", "Johnson", 35, 50));
        }};
        System.out.println("before sort: " + team);
        Collections.sort(team, new Comparator<Player>() {
            @Override
            public int compare(Player that, Player other) {
                if (that.getFieldGoalPercentage() < other.getFieldGoalPercentage()) {
                    return -1;
                } else if (that.getFieldGoalPercentage() > other.getFieldGoalPercentage()) {
                    return +1;
                } else {
                    return 0;
                }
            }
        });
        System.out.println("after  sort: " + team);
    }

    public Player(String firstName, String lastName, int numMakes, int numAttempts) {
        this.firstName = (isBlank(firstName) ? "" : firstName);
        this.lastName = (isBlank(lastName) ? "" : lastName);
        this.numAttempts = (numAttempts < 0 ? 0 : numAttempts);
        this.numMakes = (numMakes < 0 ? 0 : numMakes);
        if (this.numMakes > this.numAttempts) {
            throw new IllegalArgumentException(String.format("number of makes %d cannot exceed number of attempts %d", numMakes, numAttempts));
        }
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getNumAttempts() {
        return numAttempts;
    }

    public int getNumMakes() {
        return numMakes;
    }

    public double getFieldGoalPercentage() {
        double fieldGoalPercentage = 0.0;
        if (this.numAttempts > 0) {
            fieldGoalPercentage = ((double) numMakes)/numAttempts;
        }
        return fieldGoalPercentage;
    }

    public static boolean isBlank(String s) {
        return ((s == null) || (s.trim().length() == 0));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Player player = (Player) o;

        return firstName.equals(player.firstName) && lastName.equals(player.lastName);

    }

    @Override
    public int hashCode() {
        int result = firstName.hashCode();
        result = 31 * result + lastName.hashCode();
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("misc.Player{");
        sb.append("firstName='").append(firstName).append('\'');
        sb.append(", lastName='").append(lastName).append('\'');
        sb.append(", numAttempts=").append(numAttempts);
        sb.append(", numMakes=").append(numMakes);
        sb.append(", percentage=").append(getFieldGoalPercentage());
        sb.append('}');
        return sb.toString();
    }
}