2010-11-04 128 views
1

我希望從另一個類中的另一個方法調用的方法對象返回一個字符串。我的問題是:當我嘗試將返回的值分配給其他類中的String時,它無法在類對象中找到方法對象。Java - 返回字符串

Guessing Game = new Guessing(); 

這使得使用類Guessing.Java

else if (buttonObj == guess){ 
     double g = yourGuess.getNumber(); 
      if ((g > 0)&&(g < 11)){ 
       Game.StartGame(g); 
       label3.setVisible(false); 
       yourGuess.setEnabled(false); 
       label1.setText(Game.StartGame()); 
      }else{ 
       label3.setVisible(true); 
       yourGuess.requestFocus(true); 
      } 
    } 

當我嘗試獲取來自Guessing.Java類中StartGame方法的字符串對象,它說,它無法找到類。

public String StartGame(double guess){ 
    int round = 1; 
    int guesses = 3; 
    String correct = "correct"; 
    if (guesses > 0){ 
     if (guess == ans){ 
      correct = "correct"; 
     }else if ((guess == ans - 1)||(guess == ans + 1)){ 
      correct = "hot"; 
      guesses--; 
     }else if ((guess == ans - 2)||(guess == ans - 2)){ 
      correct = "warm"; 
      guesses--; 
     }else{ 
      correct = "cold"; 
      guesses--; 
     } 
    }else{ 
     correct = "round"; 
    } 
    return correct;   
} 

我已經嘗試了幾種不同的東西,並多次看它,但沒有什麼工作,誰能幫助?

+0

它很好地分享完整的例外。 – 2010-11-04 03:43:03

+0

先檢查你的軟件包並導入。 – Steven 2010-11-04 03:45:21

回答

1

首先通過使用這些Naming Conventions來修復您的代碼。

根據第一代碼段更改碼本,

if (buttonObj == guess){ 
     double g = yourGuess.getNumber(); 
      if ((g > 0)&&(g < 11)){ 
       String startGameStr = Game.StartGame(g); 
       label3.setVisible(false); 
       yourGuess.setEnabled(false); 
       label1.setText(startGameStr); 
      }else{ 
       label3.setVisible(true); 
       yourGuess.requestFocus(true); 
      } 
    } 
+0

謝謝,我明白了爲什麼我的代碼現在不能工作。 – Relentless 2010-11-04 03:53:28

0

很難說明導致「找不到類別」異常的具體細節。

但有一件事我可以看到它:你所呼叫的方法:

label1.setText(Game.StartGame()); 

但方法需要double說法。

+0

我知道的很多,但不管我做了什麼,即使有雙重爭論,我也無法得到它。 – Relentless 2010-11-04 03:55:08

0

有在猜測類兩個重載方法

  1. StartGame(雙G)
  2. StartGame()

似乎就像你正在調用第二種方法,當你試圖分辨re將字符串轉換爲可能會重新調用emty字符串的標籤,或者由於該方法不存在,您會收到方法找不到異常。