Question

I'm new to this whole posting thing, so forgive me if my format is not so conventional. So I have a program that is to take information from an entity class and output some random statistics that I have created and it is to loop and update the output each time. Since I am new to java I took the easy way out and used a switch statement to do this, instead of the class random.My driver looks as such:


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

   } }

It seemingly works fine until I reach the the percentage methods, then my percentages are off. Here is the Entity class:

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

And here is an example output:

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

Again I apologize for the horrible format/length of the post. I feel like it is something small I am overlooking, but if someone could point me in the right direction I would greatly appreciate it.

Était-ce utile?

La solution

The problem is that you're performing integer division. As an example, both of your variables shotsAttempted and shotsMade are integers. So let's say that shotsAttempted=8 and shotsMade=2 when you divide these you get 0.25. But that's not an integer! So Java simply takes off everything after the decimal place leaving you with a 0. To solve this, cast one of your variables to a double like this:

return ((double) shotsMade / shotsAttempted) * 100;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top