3 回答
TA贡献1804条经验 获得超8个赞
你的公式不正确。
这里:
b_YardsPerAttempt = (((float)(Yards/Attempts)- 3f) * 5f);
当您应该乘以 0.25 时,您却将 (码数/尝试次数 - 3) 的结果乘以 5。这就是你的计算出现错误的地方。
请记住,您不应以大写开头字母命名变量。此外,在计划使用新变量之前声明它们也是一个很好的做法。这是一个片段,您可以看看如何写得更好一些。
Scanner in = new Scanner(System.in);
System.out.print("Enter the full name of the quarterback: ");
String playerName = in.nextLine();
System.out.print("Enter the number of attempts: ");
Double attempts = in.nextDouble();
System.out.print("Enter the number of completions: ");
Double completion = in.nextDouble();
System.out.print("Enter the number of yards: ");
Double yards = in.nextDouble();
System.out.print("Enter the number of touchdowns: ");
Double touchdowns = in.nextDouble();
System.out.print("Enter the number of interceptions: ");
Double interceptions = in.nextDouble();
Double completionPercentage = ((completion / attempts - 0.3) * 5);
Double yardsPerAttempt = (((yards / attempts) - 3) * 0.25);
Double touchdownsPerAttempt = ((touchdowns / attempts) * 20);
Double interceptionsPerAttempt = (2.375 - ((interceptions / attempts) * 25));
Double passerRating = (((completionPercentage + yardsPerAttempt + touchdownsPerAttempt + interceptionsPerAttempt) / 6) * 100);
System.out.println("The passer rating for " + playerName + " is " + passerRating);
希望这可以帮助!
TA贡献1898条经验 获得超8个赞
我在变量及其格式的声明方面遇到了问题。
我已经修复了代码,现在如果有人感兴趣的话它可以根据需要工作,这是我的最终工作代码:
//declaring variables
double a_CompletionPercentage, b_YardsPerAttempt, c_TouchdownsPerAttempt, d_InterceptionsPerAttempt;
double PasserRating;
int Completion, Attempts, Touchdowns, Yards, Interceptions;
Scanner in = new Scanner(System.in); //create Scanner Object
//PlayerName input
System.out.print("Enter the full name of the quarterback: "); //prompt asks for player name
String PlayerName = in.nextLine(); //set string PlayerName as user's input
//attempts input
System.out.print("Enter the number of attempts: "); //prompt asks for # of attempts
Attempts = in.nextInt(); //set variable Attempts as user's input for # of attempts
//completion input
System.out.print("Enter the number of completions: ");
Completion = in.nextInt();
//yards input
System.out.print("Enter the number of yards: ");
Yards = in.nextInt();
//touchdowns input
System.out.print("Enter the number of touchdowns: ");
Touchdowns = in.nextInt();
//interceptions input
System.out.print("Enter the number of interceptions: ");
Interceptions = in.nextInt();
//calculations
if (Attempts == 0) {
System.out.println("The passer rating for " + PlayerName + " is " + 0);
} else {
a_CompletionPercentage = ((double)Completion /Attempts - 0.3) * 5.0; //formula for completion percentage
b_YardsPerAttempt = ((double)Yards /Attempts - 3.0) * 0.25; //formula for yards per attempt
c_TouchdownsPerAttempt = ((double)Touchdowns /Attempts) * 20.0; //formula for touchdowns per attempt
d_InterceptionsPerAttempt = 2.375f - ((double)Interceptions /Attempts * 25.0); //formula for interceptions per attempt
//formula for passing rate
PasserRating = (((a_CompletionPercentage + b_YardsPerAttempt + c_TouchdownsPerAttempt + d_InterceptionsPerAttempt)/6.0) * 100.0);
//Displays result
if (PasserRating < 0){
System.out.println("The passer rating for " + PlayerName + " is " + 0);
} else{
System.out.println("The passer rating for " + PlayerName + " is " + PasserRating);
}
}
TA贡献1808条经验 获得超4个赞
您没有遵循变量名称等的常用 Java 编码约定。这会使您的代码可读性较差。从学习它们开始。
像大多数新手程序员一样,您将太多精力集中在输入上,而没有足够地编写正确的函数。
现在学习JUnit还不算太早, 我建议编写五个函数:
public interface PasserRatingCalculator {
default double calculateCompletionPercentage(int numAttempts, int numCompletions) { return ((double)numCompletions)/numAttempts; }
double calculateYardsPerAttempt(int numAttempts, int numYards) { return ((double)numYards/numAttempts; }
double calculateTouuchdownsPerAttempt(int numAttempts, int numTouchdowns) { return ((double)numTouchdowns)/numAttempts; }
double calculateInterceptionsPerAttempt(int numAttempts, int numInterceptions) { return ((double)numInterceptions)/numAttempts; }
double calculatePasserRating(numAttempts, int numCompletions, int numYards, int numTouchdowns, int numCompletions);
}
添加回答
举报