2016-03-07 183 views
0

新手錯誤? 你好,我是一年級的計算機科學專業的學生,​​我一直找不到符號錯誤。我在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; 
    } 
} 
+0

你能指定你得到錯誤的代碼行嗎? – JanLeeYu

回答

0

我們需要做如下修改:

  • Resultmain()方法聲明,因此,它是局部的,只main()getScore不瞭解它。如果我們想要訪問result,inputpoints,getScore()方法,那麼我們需要將所有這些方法都傳遞給getScore()
  • getInput()返回一個輸入。所以,我們不需要通過任何爭論。我們可以改變getInput(String response)getInput()和修改main()因此通過getInput()返回值分配給input變量(input = getInput();
  • Here是傳遞Java參數的一些基礎知識。我會建議通過它。
+0

我做了修改,但在第24行我得到相同的錯誤。 –

+0

你可以用更改更新問題嗎?另外,我們在線24上有什麼? –

+0

我改變了代碼,第24行是} while(input ==「yes」);.我得到的唯一錯誤是這條線,因爲輸入變量。 –