我是 Java 新手,我一直在编写一个程序来锻炼。Bellow 是该程序的有问题的类。我正在尝试通过数组操作浮点变量,但我似乎无法以这种方式影响它们(例如数组 [i] = 1),我也无法获取它们的值(始终为 0.0),而是直接访问它们(例如变量 = 1 ) 有效。谁能告诉我我做错了什么?谢谢你。
public class Statistics {
private float switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, switchLoseP, stayWinP, stayLoseP;
private float statisticsArray[] = {switchWin, switchLose, stayWin, stayLose, gamesPlayed, switchWinP, switchLoseP, stayWinP, stayLoseP};
public void setSwitchWin() {
switchWin++;
}
public void setSwitchLose() {
switchLose++;
}
public void setStayWin() {
stayWin++;
}
public void setStayLose() {
stayLose++;
}
public void setGamesPlayed() {
gamesPlayed++;
}
public String getSwitchWinPercentage() {
return Float.toString(switchWinP = (switchWin/gamesPlayed)*100);
}
public String getSwitchLosePercentage() {
return Float.toString(switchLoseP = (switchLose/gamesPlayed)*100);
}
public String getStayWinPercentage() {
return Float.toString(stayWinP = (stayWin/gamesPlayed)*100);
}
public String getStayLosePercentage() {
return Float.toString(stayLoseP = (stayLose/gamesPlayed)*100);
}
public String getGamesPlayed() {
return Integer.toString((int) gamesPlayed);
}
public void reset() {
for(int i=0; i<statisticsArray.length; i++) {
System.out.println(statisticsArray[i]);
statisticsArray[i]=0.0f;
System.out.println(statisticsArray[i]);
}
}
}