java - 为什么这只会生成一个随机字符串? [Java]

标签 java string

public class TavernNameGen {

private static final String TXT_PLACE_NAME  = buildingText();
private static final String TXT_VERBING     = verbText();
private static final String TXT_NOUN        = nounText();

private static final String NAME =
        "Welcome to the  "+
                "\n \n "+
                TXT_PLACE_NAME+
                " of the "+
                TXT_VERBING +
                TXT_NOUN +
                "."
;


public static void read(  ) {

    GameScene.show(new WndMessage(NAME));

}


public static String buildingText()
{
    String buildingType;
    switch (Random.Int(1,4))
    {
        case 1: buildingType = "Inn";
            break;
        case 2: buildingType = "Inn";
            break;
        case 3: buildingType = "Tavern";
            break;
        case 4: buildingType = "Pub";
            break;
        default: buildingType = "Inn";
            break;
    }
    return buildingType;
}

public static String verbText()
{
    String verbName;
    switch (Random.Int(1,20))
    {
        case 1: verbName = " Dancing";
            break;
        case 2: verbName = " Prancing";
            break;
        case 3: verbName = " Eating";
            break;
        case 4: verbName = " Jigging";
            break;
        case 5: verbName = " Digging";
            break;
        case 6: verbName = " Flogging";
            break;
        case 7: verbName = " Floating";
            break;
        case 8: verbName = " Flying";
            break;
        case 9: verbName = " Laughing";
            break;
        case 10: verbName = " Smiling";
            break;
        case 11: verbName = " Drowning";
            break;
        case 12: verbName = " Golden";
            break;
        case 13: verbName = " Silver";
            break;
        case 14: verbName = " Copper";
            break;
        case 15: verbName = " Farming";
            break;
        case 16: verbName = " Running";
            break;
        case 17: verbName = " Sewing";
            break;
        case 18: verbName = " Black";
            break;
        case 19: verbName = " White";
            break;
        case 20: verbName = " Fighting";
            break;
        default: verbName = " Gesticulating";
            break;
    }
    return verbName;
}

public static String nounText()
{
    String nounName;
    switch (Random.Int(1,20))
    {
        case 1: nounName = " Pony";
            break;
        case 2: nounName = " Horse";
            break;
        case 3: nounName = " Griffin";
            break;
        case 4: nounName = " Dragon";
            break;
        case 5: nounName = " Wench";
            break;
        case 6: nounName = " Bastard";
            break;
        case 7: nounName = " Ogre";
            break;
        case 8: nounName = " Troll";
            break;
        case 9: nounName = " Ox";
            break;
        case 10: nounName = " Cow";
            break;
        case 11: nounName = " Cock";
            break;
        case 12: nounName = " Hen";
            break;
        case 13: nounName = " Ram";
            break;
        case 14: nounName = " Ewe";
            break;
        case 15: nounName = " Dog";
            break;
        case 16: nounName = " Merchant";
            break;
        case 17: nounName = " Fisherman";
            break;
        case 18: nounName = " Arborist";
            break;
        case 19: nounName = " Archer";
            break;
        case 20: nounName = " Gallbladder";
            break;
        default: nounName = " Pancreas";
            break;
    }
    return nounName;
}

}

当我运行它时,它会生成一个酒馆名称字符串。我的问题是,当我在程序中运行它时,当我再次编译程序时,它只会生成一个新字符串。如果我退出程序并再次运行它,它将生成相同的字符串。 我在这里缺少什么? 该类仅通过命令 TavernNameGen.read() 从一个外部类执行;

最佳答案

您已将变量声明为static。这意味着它们在调用过程之间保留其值,并且初始化(通过调用 verbText() 等)仅在第一次调用该方法时完成一次。删除 static 和它应该按您的预期工作。

关于java - 为什么这只会生成一个随机字符串? [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37890194/

相关文章:

java - 对于纬度/经度的距离计算,哪个更准确

javascript - 如何在不切割单词的情况下将句子拆分为具有最大字符数的句子数组?

json - Gson转换为JSON时再次转义转义序列-Android

javascript - 如何找到特定字符串并根据字符进行替换?

string - 如何使用 Flutter TextFormField 在行开头和结尾进行缩进和缩进

java - Jenkins 托管选项

java - 在jdbc中调用存储过程

java - 从 JTextField 替换一个 int

java - 用于具有多个项目的单一存储库的 Azure Pipelines 仅构建更新的一个

字符串崩溃程序中的 C# 特殊字符