数学逻辑在实体类的方法中陷入困境

时间:2014-04-03 01:45:18

标签: java pointers methods entity

我对这整个帖子都不熟悉,请原谅我,如果我的格式不那么传统的话。所以我有一个程序,它从实体类中获取信息并输出我创建的一些随机统计信息,并且每次都循环并更新输出。由于我是java的新手,我采取了简单的方法并使用switch语句来执行此操作,而不是类random.My驱动程序看起来像这样:


package program;

import java.util.Scanner; import java.util.Random;

public class Program {

   public static void main(String[] args) {
       BasketballPlayer player = new BasketballPlayer();
       player.setName("some name");
       int shots;

       for (shots = 0; shots < 25; shots++) {

           switch (shots) {
               case 1:
                   player.addMadeShot();
                   System.out.println("made 2ptr");
                   break;
               case 2:
                   player.addMadeThreePointer();
                   System.out.println("made 3ptr");
                   break;
               case 3:
                   player.addMadeShot();
                   System.out.println("made 2ptr");
                   break;
               case 4:
                   player.addMissedFreeThrow();
                   System.out.println("missed free throw");
                   break;
               case 5:
                   player.addMissedFreeThrow();
                   System.out.println("missed free throw");
                   break;
               case 6:
                   player.addMissedThreePointer();
                   System.out.println("Missed shot");
                   break;
           }
           player.printPlayerData();
       }

   } }

在我达到百分比方法之前看似工作正常,然后我的百分比关闭了。 这是实体类:

package program;

public class BasketballPlayer {

    private String playerName;
    private int shotsAttempted;
    private int threePointersAttempted;
    private int freeThrowsAttempted;
    private int shotsMade;
    private int threePointersMade;
    private int freeThrowsMade;
    private int totalPoints;

    BasketballPlayer() {

        playerName = "none";
        shotsAttempted = 0;
        shotsMade = 0;
        threePointersAttempted = 0;
        threePointersMade = 0;
        freeThrowsAttempted = 0;
        freeThrowsMade = 0;

    }

    public void setName(String newName) {
        playerName = newName;
    }

    public String getName() {
        return playerName;
    }

    public int getShotsAttempted() {
        return shotsAttempted;
    }

    public int getShotsMade() {
        return shotsMade;
    }

    public int getThreePointersAttempted() {
        return threePointersAttempted;
    }

    public int getThreePointersMade() {
        return threePointersMade;
    }

    public int getFreeThrowsAttempted() {
        return freeThrowsAttempted;
    }

    public int getFreeThrowsMade() {
        return freeThrowsMade;
    }

    public void addMissedShot() {
        shotsAttempted = (shotsAttempted + 1);

    }

    public void addMadeShot() {
        shotsAttempted = (shotsAttempted + 1);
        shotsMade = (shotsMade + 1);
        totalPoints = totalPoints +2;
    }

    public void addMissedThreePointer() {
        shotsAttempted =(shotsAttempted + 1);
        threePointersAttempted = (threePointersAttempted + 1);

    }

    public void addMadeThreePointer() {
        shotsAttempted = (shotsAttempted + 1);
        shotsMade = (shotsMade + 1);
        threePointersAttempted = (threePointersAttempted + 1);
        threePointersMade = (threePointersMade + 1);
        totalPoints = totalPoints + 3;

    }

    public void addMissedFreeThrow() {
        freeThrowsAttempted = (freeThrowsAttempted + 1);
    }

    public void addMadeFreeThrow() {
        freeThrowsAttempted = (freeThrowsAttempted + 1);
        freeThrowsMade = (freeThrowsMade + 1);
        totalPoints = totalPoints + 3;
    }

    public double shootingPercentage() {
        if (shotsAttempted == 0) {
            return 0;
        } else {
            return (shotsMade / shotsAttempted) * 100;

        }
    }

    public double threePointPercentage() {
        if (threePointersAttempted == 0) {
            return 0;
        } else {
            return (threePointersMade / threePointersAttempted) * 100;
        }
    }

    public double freeThrowPercentage() {
        if (freeThrowsAttempted == 0) {
            return 0;
        } else {
            return (freeThrowsMade / freeThrowsAttempted) * 100;
        }
    }

    public int totalPointsScored() {

           return  totalPoints; 
    }
        //((shotsMade) + (threePointersMade) + (freeThrowsMade)); 

    public void clearStats() {
        shotsAttempted = 0;
        shotsMade = 0;
        threePointersAttempted = 0;
        threePointersMade = 0;
        freeThrowsAttempted = 0;
        freeThrowsMade = 0;
    }

    public void printPlayerData() {
        System.out.printf("%13s: Shots: %1d/%1d %1.2f, Three Pointers: %1d/%1d %1.2f, Free Throws: %1d/%1d %1.2f, Total Points: %1d\n ",
                getName(), getShotsMade(), getShotsAttempted(), shootingPercentage(),
                getThreePointersMade(), getThreePointersAttempted(), threePointPercentage(), getFreeThrowsMade(),
                getFreeThrowsAttempted(), freeThrowPercentage(), totalPointsScored());
    }
}

以下是输出示例:

some name: Shots: 0/0 0.00, Three Pointers: 0/0 0.00, Free Throws: 0/0 0.00, Total Points: 0
 made 2ptr
some name: Shots: 1/1 100.00, Three Pointers: 0/0 0.00, Free Throws: 0/0 0.00, Total Points: 2
 made 3ptr
some name: Shots: 2/2 100.00, Three Pointers: 1/1 100.00, Free Throws: 0/0 0.00, Total Points: 5
 made 2ptr
some name: Shots: 3/3 100.00, Three Pointers: 1/1 100.00, Free Throws: 0/0 0.00, Total Points: 7
 missed free throw
some nameShots: 3/3 100.00, Three Pointers: 1/1 100.00, Free Throws: 0/1 0.00, Total Points: 7
 missed free throw
some name: Shots: 3/3 100.00, Three Pointers: 1/1 100.00, Free Throws: 0/2 0.00, Total Points: 7
 Missed shot
some name: Shots: 3/4 0.00, Three Pointers: 1/2 0.00, Free Throws: 0/2 0.00, Total Points: 7

我再次为帖子的可怕格式/长度道歉。我觉得这是一个我可以忽略的小事,但如果有人能指出我正确的方向,我会非常感激。

1 个答案:

答案 0 :(得分:0)

问题是您正在执行整数除法。例如,您的变量shotsAttemptedshotsMade都是整数。因此,当你将shotsAttempted=8shotsMade=2划分为0.25时,我们会说return ((double) shotsMade / shotsAttempted) * 100; 和{{1}}。但那不是一个整数!所以Java简单地取消了小数位后面的所有内容,为你留下了0.要解决这个问题,请将你的一个变量转换成这样的双精度:

{{1}}