gpt4 book ai didi

java - 计算返回无限值,为什么?

转载 作者:行者123 更新时间:2023-12-02 06:27:58 24 4
gpt4 key购买 nike

我正在为我的介绍性 java 类做最终项目,但无法弄清楚为什么我的 PercentageCap 计算得到无限值。如果您在我的代码中发现我遗漏或需要查看的内容,请告诉我。该程序必须有一个主类和两个独立的类,因此有相当多的代码!我用粗体突出显示了每节课的开始位置以及我无法弄清楚的计算。如果您对该计划还有任何其他问题,请随时在评论中留下。

演示程序

import javax.swing.JOptionPane;

// import statements

public class BaseballStatsDemo
{

public static void main(String[]args)
{

// variables

String playerName, teamName, status, experience, theNumber, theHits, theRuns, theAtBats, theHomeRuns, theRbi, theStrikeouts, theHitByPitch, theSacrificeFlies, theWalks, theBaseHits, theDoubles, theTriples, thePlayerSalary;
double hits, runs, atBats, homeRuns, rbi, strikeouts, hitByPitch, sacrificeFlies, walks, baseHits, doubles, triples, number, playerSalary, salaryCap = 0;


PlayerStats information = new PlayerStats();
FranchiseStats franchiseInfo = new FranchiseStats();
//user input

playerName = JOptionPane.showInputDialog("Enter Player Name: ");
teamName = JOptionPane.showInputDialog("Enter Team Name (IE Boston Red Sox, Los Angelos Dodges): ");

theNumber = JOptionPane.showInputDialog("Enter Player Number: ");
number = Double.parseDouble(theNumber);

status = JOptionPane.showInputDialog("Enter Player Status (Active, DL, Inactive): ");

experience = JOptionPane.showInputDialog("Rookie, Experienced, Veteran");

thePlayerSalary = JOptionPane.showInputDialog("Enter Player Salary: ");
playerSalary = Double.parseDouble(thePlayerSalary);


theHits = JOptionPane.showInputDialog("Enter Total Hits:");
hits = Double.parseDouble(theHits);

theRuns = JOptionPane.showInputDialog("Enter Total Runs:");
runs = Double.parseDouble(theRuns);

theAtBats = JOptionPane.showInputDialog("Enter Total At Bats:");
atBats = Double.parseDouble(theAtBats);

theBaseHits = JOptionPane.showInputDialog("Enter Total Singles:");
baseHits = Double.parseDouble(theBaseHits);

theDoubles = JOptionPane.showInputDialog("Enter Total Doubles:");
doubles = Double.parseDouble(theDoubles);

theTriples = JOptionPane.showInputDialog("Enter Total Triples:");
triples = Double.parseDouble(theTriples);

theHomeRuns = JOptionPane.showInputDialog("Enter Total Homeruns:");
homeRuns = Double.parseDouble(theHomeRuns);

theWalks = JOptionPane.showInputDialog("Enter Total Walks:");
walks = Double.parseDouble(theWalks);

theRbi = JOptionPane.showInputDialog("Enter Total RBI's:");
rbi = Double.parseDouble(theRbi);

theStrikeouts = JOptionPane.showInputDialog("Enter Total Strikeouts");
strikeouts = Double.parseDouble(theStrikeouts);

theHitByPitch = JOptionPane.showInputDialog("Enter Total RBI's:");
hitByPitch = Double.parseDouble(theHitByPitch);

theSacrificeFlies = JOptionPane.showInputDialog("Enter Total Sacrifice Flies:");
sacrificeFlies = Double.parseDouble(theSacrificeFlies);

information.setHits(hits);
information.setAtBats(atBats);
information.setHomeRuns(homeRuns);
information.setHitByPitch(hitByPitch);
information.setSacrificeFlies(sacrificeFlies);
information.setWalks(walks);
information.setBaseHits(baseHits);
information.setDoubles(doubles);
information.setTriples(triples);

franchiseInfo.setTeamName(teamName);
franchiseInfo.setPlayerSalary(playerSalary);
franchiseInfo.setSalaryCap(salaryCap);



JOptionPane.showMessageDialog(null, "Player Name: " + playerName +
"\nTeam Name: " + teamName +
"\nPlayer Number: " + number +
"\nStatus: " + status +
"\nExperience: " + experience +
"\nSalary: " + playerSalary +
"\n\n Player Statistics" +
"\nHits: " + hits +
"\nAt Bats: " + atBats +
"\nRuns: " + runs +
"\nBase Hits: " + baseHits +
"\nDoubles: " + doubles +
"\nTriples: " + triples +
"\nHome Runs: " + homeRuns +
"\nWalks: " + walks +
"\nRBI's: " + rbi +
"\nStrikeouts: " + strikeouts +
"\nHit By Pitch: " + hitByPitch +
"\nSacrifice Flies: " + sacrificeFlies +
"\n\n Season Averages" +
"\n Batting Average: " + information.calcAverage() +
"\n Slugging Percentage: " + information.calcSluggingPercentage() +
"\n On Base Percentage: " + information.calcOnBasePercentage() +
"\n\n Percentage of Team Salary Cap: " + **franchiseInfo.calcPercentageCap()**
);





}

}

1 级

public class PlayerStats 
{
//Attributes (fields)

private String PlayersName;
private double Hits, AtBats, HomeRuns, HitByPitch, SacrificeFlies,
Walks, BaseHits, Doubles, Triples;

//Getters and Setters

public String getPlayersName()
{
return PlayersName;
}

public void setHits (double theHits)
{
Hits = theHits;
}

public double getHits()
{
return Hits;
}

public void setAtBats (double theAtBats)
{
AtBats = theAtBats;
}

public double getAtBats()
{
return AtBats;
}

public void setHomeRuns (double theHomeRuns)
{
HomeRuns = theHomeRuns;
}

public double getHomeRuns()
{
return HomeRuns;
}

public void setHitByPitch (double theHitByPitch)
{
HitByPitch = theHitByPitch;
}

public double getHitByPitch()
{
return HitByPitch;
}

public void setSacrificeFlies (double theSacrificeFlies)
{
SacrificeFlies = theSacrificeFlies;
}

public double getSacrificeFlies()
{
return SacrificeFlies;
}

public void setWalks (double theWalks)
{
Walks = theWalks;
}

public double getWalks()
{
return Walks;
}

public void setBaseHits (double theBaseHits)
{
BaseHits = theBaseHits;
}

public double getBaseHits()
{
return BaseHits;
}

public void setDoubles (double theDoubles)
{
Doubles = theDoubles;
}

public double getDoubles()
{
return Doubles;
}

public void setTriples (double theTriples)
{
Triples = theTriples;
}

public double getTriples()
{
return Triples;
}

//Players Statistics Calculations

public double calcAverage()
{
double Average;

Average = Hits/AtBats;

return Average;
}

public double calcSluggingPercentage()
{
double SluggingPercentage;

SluggingPercentage = (BaseHits) + (2*Doubles) + (3*Triples) + (4*HomeRuns)/AtBats;

return SluggingPercentage;
}

public double calcOnBasePercentage()
{
double OnBasePercentage;

OnBasePercentage = (Hits+Walks+HitByPitch)/(AtBats+Walks+HitByPitch+SacrificeFlies);

return OnBasePercentage;
}

}

2 级

public class FranchiseStats 
{

String league, teamName;
double seriesWins, salaryCap, playerSalary;



public void setTeamName(String theTeamName)
{
teamName = theTeamName;
}

public String getTeamName()
{
return teamName;
}


public double franchiseSalaryCap()
{


if(teamName == "New York Yankees")
{
salaryCap = 228995945;
return salaryCap;
}
else if(teamName == "Boston Red Sox")
{
salaryCap = 158967286;
return salaryCap;

}
else if(teamName == "Philadelphia Phillies")
{
salaryCap = 159578214;
return salaryCap;
}
else if(teamName == "Anaheim Angels")
{
salaryCap = 142165250;
return salaryCap;
}
else if(teamName == "Chicago White Sox")
{
salaryCap = 124065277;
return salaryCap;
}
else if(teamName == "Chicago Cubs")
{
salaryCap = 104150726;
return salaryCap;
}
else if(teamName == "New York Mets")
{
salaryCap = 88877033;
return salaryCap;
}
else if(teamName == "San Francisco Giants")
{
salaryCap = 142180333;
return salaryCap;
}
else if(teamName == "Minnesota Twins")
{
salaryCap = 75562500;
return salaryCap;
}
else if(teamName == "Detroit Tigers")
{
salaryCap = 149046844;
return salaryCap;
}
else if(teamName == "St. Louis Cardinals")
{
salaryCap = 116702085;
return salaryCap;
}
else if(teamName == "Los Angeles Dodgers")
{
salaryCap = 216302909;
return salaryCap;
}
else if(teamName == "Texas Rangers")
{
salaryCap = 127197575;
return salaryCap;
}
else if(teamName == "Colorado Rockies")
{
salaryCap = 75449071;
return salaryCap;
}
else if(teamName == "Atlanta Braves")
{
salaryCap = 89288193;
return salaryCap;
}
else if(teamName == "Seattle Mariners")
{
salaryCap = 84295952;
return salaryCap;
}
else if(teamName == "Milwaukee Brewers")
{
salaryCap = 91003366;
return salaryCap;
}
else if(teamName == "Baltimore Orioles")
{
salaryCap = 91793333;
return salaryCap;
}
else if(teamName == "Cincinnati Reds")
{
salaryCap = 110565728;
return salaryCap;
}
else if(teamName == "Houston Astros")
{
salaryCap = 24328538;
return salaryCap;
}
else if(teamName == "Oakland Athletics")
{
salaryCap = 68577000;
return salaryCap;
}
else if(teamName == "Washington Nationals")
{
salaryCap = 112431770;
return salaryCap;
}
else if(teamName == "Miami Marlins")
{
salaryCap = 39621900;
return salaryCap;
}
else if(teamName == "Arizona Diamondbacks")
{
salaryCap = 90158500;
return salaryCap;
}
else if(teamName == "Cleveland Indians")
{
salaryCap = 82517300;
return salaryCap;
}
else if(teamName == "Pittsburgh Pirates")
{
salaryCap = 66289524;
return salaryCap;
}
else if(teamName =="Tampa Bay Devil Rays")
{
salaryCap = 57030272;
return salaryCap;
}
else if(teamName == "Kansas City Royals")
{
salaryCap = 80491725;
return salaryCap;
}
else if(teamName == "San Diego Padres")
{
salaryCap = 71689900;
return salaryCap;
}
else
{
return salaryCap;
}
}

public void setSalaryCap(double theSalaryCap)
{
salaryCap = theSalaryCap;
}
public double getSalaryCap()
{
return salaryCap;
}

public void setPlayerSalary (double thePlayerSalary)
{
playerSalary = thePlayerSalary;
}


public double getPlayerSalary()
{
return playerSalary;
}

public double calcPercentageCap()
{
double PercentageCap;

PercentageCap = playerSalary/salaryCap;

return PercentageCap;
}

}

最佳答案

有很多代码,并且您没有指定在哪里您获得无限值,但它很可能在您的一个部门中 - 当您 divide a double by zero 时,你会得到一个有符号的无穷大。

在除法之前确保您的股息非零。

关于java - 计算返回无限值,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20344952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com