我的getStats方法弹出无穷大数字

时间:2014-11-09 15:24:30

标签: java

在Java中,我的篮球运动员不断发回"无穷大"在我的getStats方法中,我不知道为什么。有人可以帮忙吗?我需要吸气剂和安装者,所以我有。我想测试getStats()方法,但每次都会出错。起初它是NaNa现在它的无限

public class BasketBallPlayer
{
    // instance variables - replace the example below with your own
    private String name;
    private int height; 
    private int weight;
    private double freeThrowsAttempted;
    private double freeThrowsMade;
    private double twoPointFieldGoalsAttempted;
    private double twoPointFieldGoalsMade;
    private double threePointersAttempted;
    private double threePointersMade;
    private int turnovers;
    private int assist;
    private String stats;

    public BasketBallPlayer(String name, int height, int weight, double freeThrowsMade, double twoPointFieldGoalsAttempted,double twoPointFieldGoalsMade, double threePointersAttempted, double threePointersMade, int assist, int turnovers)
    {
        //identifies the age, name, height-in., weight-lbs
        this.name=name;
        this.height=height;
        this.weight=weight;
        this.freeThrowsAttempted=freeThrowsAttempted;
        this.freeThrowsMade=freeThrowsMade;
        this.twoPointFieldGoalsAttempted=twoPointFieldGoalsAttempted;
        this.threePointersMade=threePointersMade;
        this.turnovers=turnovers;
        this.assist=assist;
    }

    public BasketBallPlayer(int weight, int height, String name)
    //identifies the weight(lbs.), height(inches) and String name
    {
        //identifies the name, height-in., weight-lbs
        this.name=name;
        this.height=height;
        this.weight=weight;

    }
    //Sets the Name
    public void setName(String name)
    {
        this.name=name;
    }
    //Sets the Height
    public void setHeight (int height)
    {
        this.height=height;
    }
    //Sets the Weight
    public void setWeight (int weight)
    {
        this.weight=weight;
    }
    //Sets the Free Throws Attempted
    public void setFreeThrowsAttempted( double freeThrowsAttempted)
    {
        this.freeThrowsAttempted=freeThrowsAttempted;
    }
    //Sets the Free Throws Made
    public void setFreeThrowsMade(double freeThrowsMade)
    {
        this.freeThrowsMade=freeThrowsMade;
    }
    // Sets two Point Field Goals Attempted
    public void setTwoPointFieldGoalsAttempted (double twoPointFieldGoalsAttempted)
    {
        this.twoPointFieldGoalsAttempted=twoPointFieldGoalsAttempted;
    }

    public void setTwoPointFieldGoalsMade (double twoPointFieldGoalsMade)
    {
        this.twoPointFieldGoalsMade=twoPointFieldGoalsMade;
    }

    public void setThreePointerAttempted(double threePointersAttempted)
    {
        this.threePointersAttempted=threePointersAttempted;
    }

    public void setThreePointersMade(double threePointersMade)
    {
        this.threePointersMade=threePointersMade;
    }

    public void setTurnovers(int turnovers)
    {
        this.turnovers=turnovers;
    }

    public void setAssist(int assist)
    {
        this.assist=assist;
    }
    //Returns a Name
    public String getName()
    {
        return name;
    }

    public int getHeight ()
    {
        return height;
    }

    public int getWeight ()
    {
        return weight;
    }

    public double getFreeThrowsAttempted()
    {
        return freeThrowsAttempted;
    }

    public double getfreeThrowsMade()
    {
        return freeThrowsMade;
    }

    public double getTwoPointFieldGoalsAttempted ()
    {
        return twoPointFieldGoalsAttempted;
    }

    public double getTwoPointFieldGoalsMade ()
    {
        return twoPointFieldGoalsMade;
    }

    public double getThreePointerAttempted()
    {
        return threePointersAttempted;
    }

    public double getthreePointersMade()
    {
        return threePointersMade;
    }

    public int getTurnovers()
    {
        return turnovers;
    }

    public int gettAssist()
    {
        return assist;
    }

    /** The geStats Method allows you to get all information on the player in print. All Percentages
     * 
     */
    public void getStats()
    {
        double Percentage1;
        double Percentage2;
        double  Percentage3;
        Percentage1=(double)((twoPointFieldGoalsMade*100)/twoPointFieldGoalsAttempted);
        Percentage2=((double)(threePointersMade*100)/threePointersAttempted);
        Percentage3=((double)((freeThrowsMade*100)/freeThrowsAttempted));
        System.out.println("*************************");
        System.out.println("BasketBall Player Name:" + name);
        System.out.println("Field Goal Percentage:" + Percentage1 +"%");
        System.out.println("3 Pointer Percentage:" + Percentage2 +"%");
        System.out.println("Free Throw Percentage:" + Percentage3 +"%");
        System.out.println("Assist to Turnover Ration:" + assist/turnovers);
        System.out.println("*************************");
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您不应该使用double来获取这些号码。它只是考虑到效率低下而没有任何意义。但是,在使用int时,您必须注意,在划分两个int时,您无法直接获得百分比。这段代码应该可以工作,你可能想要包含一些检查除数不是0的代码。

private int freeThrowsAttempted;
private int freeThrowsMade;
private int twoPointFieldGoalsAttempted;
private int twoPointFieldGoalsMade;
private int threePointersAttempted;
private int threePointersMade;

...

double percentage1 = (twoPointFieldGoalsMade * 100F) / twoPointFieldGoalsAttempted;
double percentage2 = (threePointersMade * 100F) / threePointersAttempted;
double percentage3 = (freeThrowsMade * 100F) / freeThrowsAttempted;