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);? ? ? ??
}
添加回答
舉報