如何将方法的输出返回到Java中的main方法?

时间:2016-03-29 05:16:29

标签: java

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

    public class Assignment51
    {
       public static void main(String[] args)
       {
          /*
          Four Categories are used as a basis for compiling a rating:
          1. Percentage of completions
          2. Average yards gained
          3. Percentage of touchdown passes per attempt
          4. Percentage of interceptions per attempt
          */

          // DecimalFormat formatter = new DecimalFormat("#,##0.000");

          String input;        //get user input
          String name;         //player name
          int completion;      //number of completions in percentages
          int yardsGained;     //number of yards gained
          int tdpasses;        //number of touchdown passes
          int interceptions;   //number of interceptions
          int attempts;        //number of attempts
          int yards;           //number of yards
          double score;        //passer score
          double totalScore;   //total score
          double totalPoints;  //total award points
          double total;        //total sum

          //get player name
          name = JOptionPane.showInputDialog("Type in the player name");


          //Creating Methods

          PctCompletion();
          AvgYardsGain();
          PctTouchdown();
          PctInterceptions();     


          //RETURN TO MAIN METHOD
          double PctCompletion, AvgYardsGain, PctTouchdown, PctInterceptions;

          totalPoints = PctCompletion +  AvgYardsGain + PctTouchdown + PctInterceptions;

          total = (totalPoints / 6) * 100;

          //OUTPUT      
          JOptionPane.showMessageDialog(null, "total points: " + totalPoints + "\ntotal: " + total);
          //JOptionPane.showMessageDialog(null, name + " earned a total of " + total + " award points.\n"); //GET HELP WITH OUTPUT
       }


       //calling the PctCompletion method.
       public static double PctCompletion()
       { 
          String input;                     //user input
          int num1;                         //first input
          int num2;                         //second input total amount of passes
          double percentComp = 0.0;         //percent completed. num1/num2 = percentComp
          double pointRatingComp = 0.0;     //Completions point rating
          double awardPoints1 = 0.0;         //awarded points +/- 2.375

          input = JOptionPane.showInputDialog("Type in the TOTAL number of passes");
          num2  = Integer.parseInt(input);

          input = JOptionPane.showInputDialog("Type in the number of COMPLETED passes");
          num1 = Integer.parseInt(input);

          percentComp = ((num1 / num2) * 100);

          //point rating
          pointRatingComp = ((percentComp - 30) * .05);

          if (pointRatingComp < 0 && percentComp < 30.0)
             awardPoints1  = 0.0;

          if (pointRatingComp > 2.375 && percentComp > 77.5)
             awardPoints1 = 2.375;

          return awardPoints1;      
       }


       //calling the AvgYardsGain method
       public static double AvgYardsGain()
       {
          String input;                     //user input
          int num1;                         //first input total yards
          int num2;                         //second input total attempts
          double percentYard = 0.0;         //percent completed. num1/num2 = percentYard
          double pointRatingYard = 0.0;     //Yards point rating
          double awardPoints2 = 0.0;        //awarded points +/- 2.375

          input = JOptionPane.showInputDialog("Type in the TOTAL number of attempts");
          num2 = Integer.parseInt(input);

          input = JOptionPane.showInputDialog("Type in the TOTAL amount of yards");
          num1 = Integer.parseInt(input);

          percentYard = ((num1 / num2) * 100); 

          //point rating
          pointRatingYard = ((percentYard - 3) * .25);

          if (pointRatingYard < 0 && percentYard < 3)
             awardPoints2 = 0;

          if (pointRatingYard > 2.375 && percentYard > 12.5)
             awardPoints2 = 2.375;

          return awardPoints2;
       }


       //calling the PctTouchdown method
       public static double PctTouchdown()
       {
          String input;                      //user input
          int num1;                          //input total number of touchdowns
          int num2;                          //input total amount of attempts
          double percentTD = 0.0;            //percent completed. num1/num2 = percentTD
          double pointRatingTD = 0.0;        //touchdowns point rating
          double awardPoints3 = 0.0;         //awarded points +/- 2.375

          input = JOptionPane.showInputDialog("Type in the TOTAL number of pass ATTEMPTS");
          num2 = Integer.parseInt(input);

          input = JOptionPane.showInputDialog("Type in the TOTAL number of TOUCHDOWNS");
          num1 = Integer.parseInt(input);

          percentTD = ((num1 / num2) * 100);

          //point rating
          pointRatingTD = (percentTD * .2);

          if (pointRatingTD > 2.375 && percentTD > 11.875)
             awardPoints3 = 2.375;

          return awardPoints3;     
       }


       //calling the PctInterceptions method
       public static double PctInterceptions()
       {
          String input;                     //user input
          int num1;                         //input total amount of interceptions
          int num2;                         //input total amount of attempts
          double percentINT = 0.0;         //percent completed. num1/num2 = percentComp
          double pointRatingINT = 0.0;     //Completions point rating
          double awardPoints4 = 0.0;         //awarded points +/- 2.375

          input = JOptionPane.showInputDialog("Type in the TOTAL number of pass ATTEMPTS");
          num2 = Integer.parseInt(input);

          input = JOptionPane.showInputDialog("Type in the TOTAL amount of INTERCEPTIONS");
          num1 = Integer.parseInt(input);

          percentINT = ((num1 / num2) * 100);

          //point rating
          pointRatingINT = 2.375 - (percentINT * .25);

          if (pointRatingINT < 0 && percentINT > 9.5)
             awardPoints4 = 0;
          else
             awardPoints4 = 2.375;

          return awardPoints4;
       }
    }

我希望返回值(奖励点)根据从方法中获得的值进行计算。但是,当我这样做时,我收到了一个错误。 这是我遇到问题的代码的一部分。

//RETURN TO MAIN METHOD
              double PctCompletion, AvgYardsGain, PctTouchdown, PctInterceptions;

              totalPoints = PctCompletion +  AvgYardsGain + PctTouchdown + PctInterceptions;

              total = (totalPoints / 6) * 100;

2 个答案:

答案 0 :(得分:0)

调用函数时需要使用括号

totalPoints = PctCompletion +  AvgYardsGain + PctTouchdown + PctInterceptions;

应该更正

totalPoints = PctCompletion() +  AvgYardsGain() + PctTouchdown() + PctInterceptions();

答案 1 :(得分:0)

您应该将返回值分配给变量,然后使用这些变量 请参阅以下代码:

    // Creating Methods

    double PctCompletion = PctCompletion();
    double AvgYardsGain = AvgYardsGain();
    double PctTouchdown = PctTouchdown();
    double PctInterceptions = PctInterceptions();

    // RETURN TO MAIN METHOD

    totalPoints = PctCompletion + AvgYardsGain + PctTouchdown + PctInterceptions;

    total = (totalPoints / 6) * 100;