How to return a string?

前端 未结 2 626
遥遥无期
遥遥无期 2021-01-14 23:11
import java.util.*;
public class HangManP5 
{
public static void main(String[] args) 
{
int attempts = 10;
int wordLength;
boolean solved;
Scanner k = new Scanner(Sy         


        
2条回答
  •  深忆病人
    2021-01-14 23:31

    You can't modify the caller's reference.

    RandomWord(word);
    

    needs to be something like

    word = RandomWord(word);
    

    Also, by convention, Java methods start with a lower case letter. And, you could return the word without passing one in as an argument and I suggest you save your Random reference and use an array like

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

    And then call it like

    word = randomWord();
    

提交回复
热议问题