2015-01-12 119 views
2
import java.util.*; 
public class HangManP5 
{ 
public static void main(String[] args) 
{ 
int attempts = 10; 
int wordLength; 
boolean solved; 
Scanner k = new Scanner(System.in); 
System.out.println("Hey, what's your name?"); 
String name = k.nextLine(); 
System.out.println(name+ ", hey! This is a hangman game!\n"); 
RandomWord(word); 
int len = word.length(); 
char[] temp = new char[len]; 
for(int i = 0; i < temp.length; i++) 
{ 
    temp[i] = '*'; 
} 
System.out.print("\n"); 
System.out.print("Word to date: "); 
while (attempts <= 10 && attempts > 0) 
{ 
    System.out.println("\nAttempts left: " + attempts); 
    System.out.print("Enter letter: "); 
    String test = k.next(); 
    if(test.length() != 1) 
    { 
     System.out.println("Please enter 1 character"); 
     continue; 
    } 
    char testChar = test.charAt(0); 
    int foundPos = -2; 
    int foundCount = 0; 
    while((foundPos = word.indexOf(testChar, foundPos + 1)) != -1) 
    { 
     temp[foundPos] = testChar; 
     foundCount++; 
     len--; 
    } 
    if(foundCount == 0) 
    { 
     System.out.println("Sorry, didn't find any matches for " + test); 
    } 
    else 
    { 
     System.out.println("Found " + foundCount + " matches for " + test); 
    } 

    for(int i = 0; i < temp.length; i++) 
    { 
     System.out.print(temp[i]); 
    } 
    System.out.println(); 

    if(len == 0) 
    { 
     break; //Solved! 
    } 
    attempts--; 
} 

if(len == 0) 
{ 
    System.out.println("\n---------------------------"); 
    System.out.println("Solved!"); 
} 
else 
{ 
    System.out.println("\n---------------------------"); 
    System.out.println("Sorry you didn't find the mystery word!"); 
    System.out.println("It was \"" + word + "\""); 
} 
} 
public static String RandomWord(String word) 
{ 
//List of words 
Random r = new Random(); 
int a = 1 + r.nextInt(5); 
if(a == 1) 
{ 
    word=("Peace"); 
} 
if(a == 2) 
{ 
    word=("Nuts"); 
} 
if(a == 3) 
{ 
    word=("Cool"); 
} 
if(a == 4) 
{ 
    word=("Fizz"); 
} 
if(a == 5) 
{ 
    word=("Awesome"); 
} 
return (word); 
} 
} 

好的,所以這是我的hang子手遊戲的代碼,我唯一要做的就是讓我的程序隨機化一個單詞,它應該在方法中成功執行。但我遇到的唯一問題是獲取字符串變量「字」回到主類(在主類中有所有「字」變量強調的錯誤)。如何返回一個字符串?

如果我可以用這個或另一種方式從列表中產生一個隨機單詞,這將是驚人的。

+1

沒有理由認爲'RandomWord'應該採取的參數'串word'。您不使用該參數。你的語法錯誤可能與此有關。 –

+1

您似乎誤解了如何在Java中聲明變量:' = '。所以,'RandomWord(word)'不是用隨機詞初始化'字串'的正確方法。相反,你應該使用'String word = RandomWord()'(假設你刪除了String參數,就像Eric提到的那樣) – Vulcan

+0

哦,我沒有聲明變量,我正在調用這個方法@Vulcan – BuySomeChips

回答

3

在java中,參數是通過價值傳遞而不是通過參考。因此,您無法更改參數的參考。

在你的情況,你需要做的:

public static String getRandomWord() { 
    switch(new Random().nextInt(5)) { 
     case 0: 
      return "Peace"; 
     case 1: 
      return "Nuts"; 
     // ... 
     default: 
      throw new IllegalStateException("Something went wrong!"); 
    } 
} 

而且在main

// ... 
String word = getRandomWord(); 
int len = word.length(); 
// ... 
+0

它的工作!非常感謝! :) – BuySomeChips

2

您不能修改調用者的參考。

RandomWord(word); 

必須像

word = RandomWord(word); 

另外,按照慣例,Java方法以小寫字母開頭。而且,你可能會返回word沒有通過一個作爲參數,我建議您保存Random參考和使用數組一樣

private static Random rand = new Random(); 
public static String randomWord() { 
    String[] words = { "Peace", "Nuts", "Cool", "Fizz", "Awesome" }; 
    return words[rand.nextInt(words.length)]; 
} 

然後調用它像

word = randomWord(); 
+1

嘿,謝謝!它幫助清除了一切! – BuySomeChips