在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(); // ...



