Accessing float variable through java array
I'm new to Java and i have been writing a program to exercise. Bellow is
the problematic class of the program. I'm trying to manipulate float
variables via the array, but i cant seem to affect them that way (e.g.
array[i] = 1) nor can i get their values(always 0.0), but accessing them
directly (e.g. variable = 1) works. Can anyone tell me what am I doing
wrong? Thank you.
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]);
}
}
}
No comments:
Post a Comment