我对整个发帖的事情很陌生,所以如果我的格式不那么传统,请原谅我。所以我有一个程序,它从实体类中获取信息并输出我创建的一些随机统计数据,并且每次都会循环并更新输出。由于我是 java 新手,所以我采取了简单的方法并使用 switch 语句来执行此操作,而不是使用 random 类。我的驱动程序如下所示:
<小时/>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
我再次对帖子的糟糕格式/长度表示歉意。我觉得我忽略了一些小事情,但如果有人能指出我正确的方向,我将不胜感激。
最佳答案
问题是您正在执行整数除法。例如,变量 shotsAttempted
和 shotsMade
都是整数。因此,假设将 shotsAttempted=8
和 shotsMade=2
相除,得到 0.25
。但这不是一个整数!因此,Java 只是去掉小数点后的所有内容,留下 0。要解决此问题,请将其中一个变量转换为 double 型,如下所示:
return ((double) shotsMade / shotsAttempted) * 100;
关于java - 实体类方法中的数学逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826331/