如何计算和显示 NFL 传球手评分?

我正在尝试做这个家庭作业问题,但我在设置它和理解如何开始和完成这些结果时遇到困难。

到目前为止,我已经声明了公式的变量,创建了扫描仪并尝试编写公式。这是我到目前为止所拥有的:

 //declaring variables

  float a_CompletionPercentage, b_YardsPerAttempt, c_TouchdownsPerAttempt, d_InterceptionsPerAttempt;

  float PasserRating;

  double 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.nextDouble(); //set variable Attempts as user's input for # of attempts


  //completion input

  System.out.print("Enter the number of completions: "); 

  Completion = in.nextDouble(); 


  //yards input

  System.out.print("Enter the number of yards: "); 

  Yards = in.nextDouble();


  //touchdowns input

  System.out.print("Enter the number of touchdowns: "); 

  Touchdowns = in.nextDouble(); 


  //interceptions input

  System.out.print("Enter the number of interceptions: "); 

  Interceptions = in.nextDouble(); 


我不确定我的变量是否已正确声明并且我的公式不起作用。


这些是我应该得到的一些示例输出:输出示例 1:


输入四分卫的全名:Jameis Winston


输入尝试次数:35


输入完成次数:22


输入码数:345


输入达阵次数:4


输入拦截次数:1


Jameis Winston 的传球者评分为 121.72619047619047


但我得到的是 664.58325 而不是 121.72619047619047。



HUH函数
浏览 141回答 3
3回答

胡说叔叔

你的公式不正确。这里: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);希望这可以帮助!

汪汪一只猫

我在变量及其格式的声明方面遇到了问题。我已经修复了代码,现在如果有人感兴趣的话它可以根据需要工作,这是我的最终工作代码: //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);     }  }

炎炎设计

您没有遵循变量名称等的常用 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);        }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java