错误:传递和返回变量时找不到符号

时间:2016-03-07 00:33:21

标签: java parameter-passing cannot-find-symbol

新秀的错误? 您好,我是一名计算机科学专业的一年级学生,我一直找不到符号错误。我在main方法中声明了变量,将其传递给另一个方法,修改它,然后返回它。由于某种原因,编译器无法找到符号结果,输入和点。我确定所有这些原因都是一样的。任何帮助将不胜感激。

public class Fishing
{
   public static void main(String[] args)
   {
      do
      {
         String input;     //Holds user input
         int points;      // Holds player's points
         int score = 0;   // Sets player's score to 0
         final int DIE_SIDES = 6;    // # of sides for the die

         //Create an instance of the Die class
         Die die = new Die(DIE_SIDES);

         //Roll the die once and store value in result
         die.roll();
         int result = die.getValue();

         getScore(points, result);
         String input = getInput();

         //Keeps running total of player's score
         score = score + points;

      } while (input == "yes");
      System.out.print(0);
   }

   /**
     The getScore method will calculate the player's score
     depending on what the player rolled. It will also show
     a message and return the score.
     @return A reference to an integer object containing
             the player's score for one roll.
   */

   public static int getScore(int points, int result)
   {
      if (result == 1)
      {
         JOptionPane.showMessageDialog(null, "Waaaaahhhhh, you have caught " +
                                   "a shark. Sharks are dangerous. You " +
                                        "have been awarded zero points.");
         points = 0;
         return points;
     }
     else if (result == 2)
     {
         JOptionPane.showMessageDialog(null, "You have caught a jellyfish. " +
                              "This beautiful creature has awarded you " +
                                                           "50 points!!");
         points = 50;
         return points;
     }
     else if (result == 3)
     {
         JOptionPane.showMessageDialog(null, "You have caught an old boot. " +
                            "Maybe you can sell this old boot after it " +
                             "dries out. You have been awarded 1 point.");
         points = 1;
         return points;
     }
     else if (result == 4)
     {
         JOptionPane.showMessageDialog(null, "You have caught an Alaskan salmon. " +
                            "This delicious and popular fish has awarded you " +
                                                                "75 points!!!");
         points = 75;
         return points;
     }  
     else if (result == 5)
     {
         JOptionPane.showMessageDialog(null, "You have caught a small fish. You " +
                                               "have been awarded 20 points!");                                                                                
         points = 20;
         return points;
     }
     else
     {
         JOptionPane.showMessageDialog(null, "You have caught a treasure chest!! " +
                              "It is filled with shining pieces of gold, and " +
                                        "you have been awarded 100 points!!!!");
         points = 100;
         return points;
     }
  }

   /**
      The getInput method will receive the user's input
      and return it to the main method.
      @return A reference to a String input value containing
              the user's response.
   */  

    public static String getInput()
    {
        //Prompt user to enter response
        response = JOptionPane.showInputDialog("Would you like to play another " +
                                   "round of fishing? Enter yes or no.");
        return response;
    }
}

1 个答案:

答案 0 :(得分:0)

我们需要进行以下更改:

  • Resultmain()方法中声明,因此仅在main()为本地。 getScore不了解它。如果我们想要在result方法中访问inputpointsgetScore(),那么我们需要将所有这些内容传递给getScore()
  • getInput()返回一个输入。所以,我们不需要传递任何参数。我们可以将getInput(String response)更改为getInput()并修改main(),以便将getInput()返回的值分配给input变量(input = getInput();
  • Here是java中参数传递的一些基础知识。我建议通过它。