问题:我正在尝试找到胜率(胜率百分比的公式为wins/(wins + loses)
)。如何从胜利中获取值并输掉用户输入并将其添加到我的Sysout函数中。每次运行程序时都会显示:
East W L PCT
Braves 45 66 0.000000
Cubs 87 77 0.000000
所以我要做的是获取实际值,而不是说“0.0000000”
public class Team {
// Data fields...
private int wins;
private int loses;
private String teamName;
private String city;
private String division;
private double winPercentage;
// Getters and setters...
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLoses() {
return loses;
}
public void setLoses(int loses) {
this.loses = loses;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public double getWinPercentage() {
return wins/(wins + loses);
}
public void setWinPercentage(double winPercentage) {
this.winPercentage = winPercentage;
}
}
public class PlayoffSelectorClass extends Team{
public static void main(String[] args) {
List<Team> teams = new ArrayList<Team>();
for (int i = 0; i < 6; i++) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter team name: ");
String name = input.nextLine();
System.out.println("\nPlease enter the city " + name + " played in: ");
String city = input.nextLine();
System.out.println("\nPlease enter the division " + name + " play in: ");
String division = input.nextLine();
System.out.println("\nPlease enter the number of wins " + name + " has: ");
Integer wins = input.nextInt();
System.out.println("\nPlease enter the number of losses " + name + " has: ");
Integer loses = input.nextInt();
if (i < 5) {
System.out.println("\nEnter your next team...\n");
}
Team team = new Team();
team.setTeamName(name);
team.setCity(city);
team.setDivision(division);
team.setWins(wins);
team.setLoses(loses);
team.setWinPercentage(wins / (wins + loses));
teams.add(team);
}
System.out.println("East W L PCT\n");
for (Team team : teams) {
System.out.printf("%s\t%s\t%s\t%f\n",team.getTeamName() + " ", team.getWins() + " " , team.getLoses(), team.getWinPercentage());
}
}
}
答案 0 :(得分:0)
问题在于:
public double getWinPercentage() {
return wins/(wins + loses);
}
您正在进行整数除法,即仅保留结果的int部分,并将其隐式地转换为double
。例如,如果:
wins = 10;
loses = 20;
然后winPercentage == 10/30 == 0.33..
,您只保留0
并将其转换为double
。因此,0.0000...
等
你可以做的是:
public double getWinPercentage() {
return wins/(double) (wins + loses);
}