为什么这只生成一个随机字符串? 【JAVA]

时间:2016-06-17 20:49:59

标签: 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();

从一个外部类执行

1 个答案:

答案 0 :(得分:2)

您已声明变量static。这意味着它们在对过程的调用之间保留它们的值,并且初始化(通过调用verbText()等仅在第一次调用方法时完成ONCE。删除static它应该工作如你所料。