2017-09-06 38 views
0

我對Java編程還很新,我正在嘗試構建一個文本遊戲來幫助我學習。我遇到問題時將一個變量傳遞給我,幫助我測試規格。如何在java中傳輸變量

String a = user.next(); 

    if(a.equals("warrior")){ 
      System.out.println("You start to feel stronger, feels like you could run through a brick wall and keep on going without a scrath on you.\n" + 
       "Of course you might want to wait on that for awhile.\n" + 
       "********************************************************\n*\t\tAchievement got: Dumb as a rock!\t\t\t\t\t\t *\n********************************************************"); 
      String specW = ("warrior"); 
      comW (warrior); 
    } 

    public static void comW (warrior){ 
    System.out.println("Testing1"); 
    } 

是任何人能幫助我弄清楚爲什麼我一直有錯誤?

+0

'公共靜態無效comW(勇士){'你需要給一個類型爲'warrior' – litelite

+0

意味着什麼litelite是你應該String類型添加到方法的簽名:'公共靜態無效comW(字符串戰士)' – alfasin

+0

你似乎需要閱讀一些Tutos,有很多錯誤 – azro

回答

1
comW (specW); 
} 

public static void comW (String warrior){ 
System.out.println(warrior); 
} 

在代碼中進行上述更改。 基本上你在comW方法中傳遞一個變量,但該變量沒有聲明。其次,在你的comW方法中,你沒有給出參數類型。

我會建議您在這裏張貼問題之前,請仔細遵循一些教程。

0

要創建一個使用名稱specW一個字符串變量,但傳遞變量戰士comW功能。因此請將功能參數更改爲specW。另外在功能comW參數戰士的定義沒有給出任何類型,以便給它一個字符串類型。

String a = user.next(); 

if(a.equals("warrior")){ 
     System.out.println("You start to feel stronger, feels like you could run through a brick wall and keep on going without a scrath on you.\n" + 
      "Of course you might want to wait on that for awhile.\n" + 
      "********************************************************\n*\t\tAchievement got: Dumb as a rock!\t\t\t\t\t\t *\n********************************************************"); 
     String specW = ("warrior"); 
     comW (specW); // 1st Change 
} 

public static void comW (String warrior){ // 2nd Change 
System.out.println("Testing1"); 
}