java - 如何返回一个字符串?

标签 java string

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);
 }
 }

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

如果我能通过这种或其他方式从列表中生成随机单词获得帮助,那就太棒了。

最佳答案

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

关于java - 如何返回一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893989/

相关文章:

java - 如何从 JBoss 外部与 Infinispan 通信?

ruby - 如何将 BigDecimal 转换为两位小数的字符串?

C++: std::string 问题

c - 在这种情况下,从用户在 C 中输入的字符串中提取子字符串的最佳方法是什么

java - 如何在枚举单例中实现日志记录?

java - 通过 2 个点画一条无限长的线?

java - Java 中的类似接口(interface)问题

java - Selenium Java findElement 查询对于唯一 ID 或 Xpath 无效

c# - 换行符未在文本框中正确显示

C 字符串特殊字符(葡萄牙语)