如何返回字符串? import java.util.*; 公共级刽子手5 { 公共静态void main(字符串[]args) { int=10; int字长; 布尔解; 扫描仪k=新的扫描仪(System.in); System.out.println(“嘿,你叫什么名字?”); 字符串名称=k.nextLine(); System.out.println(name+“,嘿!这是一个刽子手游戏!\n”); 随机词; int len=word.length(); char[]temp=新字符[len]; 对于(int i=0;i

如何返回字符串? import java.util.*; 公共级刽子手5 { 公共静态void main(字符串[]args) { int=10; int字长; 布尔解; 扫描仪k=新的扫描仪(System.in); System.out.println(“嘿,你叫什么名字?”); 字符串名称=k.nextLine(); System.out.println(name+“,嘿!这是一个刽子手游戏!\n”); 随机词; int len=word.length(); char[]temp=新字符[len]; 对于(int i=0;i,java,string,Java,String,好的,这是我为一个刽子手游戏编写的代码,我唯一要做的就是让我的程序将其中一个单词随机化,它应该在方法中成功地做到这一点。但是我遇到的唯一问题是让字符串变量“word”返回到主类(在主类中的所有“word”变量下面都有错误) 如果我能从列表中以这种或另一种方式得到帮助来生成一个随机单词,那就太棒了。你不能修改呼叫者的引用 import java.util.*; public class HangManP5 { public static void main(String[] args) { i

好的,这是我为一个刽子手游戏编写的代码,我唯一要做的就是让我的程序将其中一个单词随机化,它应该在方法中成功地做到这一点。但是我遇到的唯一问题是让字符串变量“word”返回到主类(在主类中的所有“word”变量下面都有错误)


如果我能从列表中以这种或另一种方式得到帮助来生成一个随机单词,那就太棒了。

你不能修改呼叫者的引用

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);
 }
 }
应该是这样的

RandomWord(word);
此外,按照惯例,Java方法以小写字母开头。而且,您可以返回
单词
,而无需将其作为参数传入,我建议您保存
随机
引用,并使用如下数组

word = RandomWord(word);
然后像这样称呼它

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

在java中,参数是通过值传递的,而不是通过引用传递的。因此,不能更改参数的引用

在您的情况下,您需要执行以下操作:

word = randomWord();

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

没有理由
RandomWord
应采用参数
String word
。不使用该参数。您的语法错误可能与此有关。您似乎误解了如何在Java中声明变量:
=
。因此,
RandomWord(word)
不是用随机词初始化
字符串单词的正确方法。相反,您应该使用
String word=RandomWord()
(假设您删除了字符串参数,正如Eric提到的。哦,我不是在声明变量,而是在调用方法@Vulcan@BuySomeChips我可以看到。如果你想对方法的输出做任何事情,你需要声明一个变量并将其分配给方法的输出。请随意将你的工作代码发布到-很多人都有从那里的Java社区学到了很多东西;)
// ...
String word = getRandomWord();
int len = word.length(); 
// ...