Java 我的getStats方法会弹出无穷多个数字

Java 我的getStats方法会弹出无穷多个数字,java,Java,在Java中,我的篮球运动员在我的getStats方法中不断返回“infinity”,我不知道为什么。有人能帮忙吗?我需要接球手和二传手,所以我有。我想测试getStats()方法,但每次都会出错。起初它是娜娜,现在它是无限的 public class BasketBallPlayer { // instance variables - replace the example below with your own private String name; private

在Java中,我的篮球运动员在我的getStats方法中不断返回“infinity”,我不知道为什么。有人能帮忙吗?我需要接球手和二传手,所以我有。我想测试getStats()方法,但每次都会出错。起初它是娜娜,现在它是无限的

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("*************************");
    }
}

首先,您不应该对这些数字使用
double
s。这只会导致效率低下,毫无意义。但是,当使用
int
s时,您必须注意,当除以两个
int
s时,您不能直接得到一个百分比。这段代码应该可以工作,您可能需要包含一些代码来检查除数是否为
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;

如果玩家没有尝试任何东西,它应该打印什么?为什么要用双精度表示多次尝试/点数?是否存在一半尝试/分数?我刚刚注意到。我有一个“测试者”来测试它是否有效