How to return a string?

前端 未结 2 608
遥遥无期
遥遥无期 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:26

    In java, parameters are passed by value and not by reference. Therefore, you cannot change the reference of a parameter.

    In your case, you need to do:

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

    And in main:

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

提交回复
热议问题