2016-06-22 90 views
1

我在這裏尋找答案(Java: how to "restart" a static class?),但它似乎沒有回答我的問題。如何重置此隨機字符串生成器? [Java]

我有一個類從單詞列表中生成一個有點隨機的字符串。 然後通過read()將單詞輸出到遊戲中的標誌上。 我可以用兩種方法做事:如果buildingText(),verbText()和nounText()不是靜態的,每次訪問符號時都會得到一個新字符串。 如果這三者是靜態的,即使在遊戲重新啓動後,我也會得到一個不會改變的字符串。

如何更改此代碼以便reset()創建一個新的隨機字符串?

我一直在試圖使用布爾和圍繞開關語句與if,但似乎不可能。

下面的代碼:

public class Sign { 

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


protected static final String SIGNS = 
     "Welcome to the "+ "\n \n "+ TXT_PLACE_NAME+ " of the "+ TXT_VERBING + TXT_NOUN + ""; 



public void reset(){ 
    //still working on this. As it stands, will only reset if you exit the program 
    //could be worse things 
} 

public void read(int pos) { 
    GameScene.show(new WndMessage(SIGNS)); 


} 

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

編輯:

這是一種 '最佳的' 解決方案。它整合了以下兩種解決方案的元素併產生期望的結果。請隨意批評格式和組織!

public class Sign { 


protected static String SIGNS; 
protected static final String SIGNS_FORMAT = "Welcome to the %s of the %s %s"; 

private static final String[] BUILDING_TYPES = new String[]{ 
     "Inn", 
     "Inn", 
     "Tavern", 
     "Pub", 
     "Inn" 
}; 

private static final String[] VERB_NAMES = new String[] { 
     "Dancing", 
     "Prancing", 
     "Eating", 
     "Jigging", 
     "Digging", 
     "Flogging", 
     "Floating", 
     "Flying", 
     "Laughing", 
     "Smiling", 
     "Drowning", 
     "Golden", 
     "Silver", 
     "Copper", 
     "Farming", 
     "Running", 
     "Sewing", 
     "Black", 
     "White", 
     "Fighting", 
     "Gesticulating" 
}; 

private static final String[] NOUN_NAMES = new String[] { 
     "Pony", 
     "Horse", 
     "Griffin", 
     "Dragon", 
     "Wench", 
     "Bastard", 
     "Ogre", 
     "Troll", 
     "Ox", 
     "Cow", 
     "Cock", 
     "Hen", 
     "Ram", 
     "Ewe", 
     "Dog", 
     "Merchant", 
     "Fisherman", 
     "Arborist", 
     "Archer", 
     "Gallbladder", 
     "Pancreas" 
}; 

private String currentSign = getNewSign(); 

public void reset(){ 
    buildingText(); 
    verbText(); 
    nounText(); 
    SIGNS = currentSign; 
} 

public void read(int pos) { 
    //referenced by pos in order to make the sign catch fire later on 
    GameScene.show(new WndMessage(SIGNS)); 
} 

private String getNewSign(){ 
    return String.format(SIGNS_FORMAT, buildingText(), verbText(), nounText()); 
} 

private String buildingText() { 
    return BUILDING_TYPES[Random.Int(0, BUILDING_TYPES.length - 1)]; 
} 

private String verbText() { 
    return VERB_NAMES[Random.Int(0, VERB_NAMES.length - 1)]; 
} 

private String nounText() { 
    return NOUN_NAMES[Random.Int(0, NOUN_NAMES.length - 1)]; 
} 

}

+0

爲什麼不將這種行爲轉移到對象中?然後你可以構造一個新的對象實例。 – dimo414

回答

0

這感覺有點像你想濫用靜態變量,但你可以這樣來做。

public class Sign { 

// These are no longer final as you need to change them 
private static String TXT_PLACE_NAME; 
private static String TXT_VERBING; 
private static String TXT_NOUN; 
protected static String SIGNS; 


// Since they're not final reset can change them. 
public void reset(){ 
    TXT_PLACE_NAME  = buildingText(); 
    TXT_VERBING   = verbText(); 
    TXT_NOUN   = nounText(); 
    SIGNS = "Welcome to the "+ "\n \n "+ TXT_PLACE_NAME+ " of the "+ TXT_VERBING + TXT_NOUN + ""; 
} 

// This ensures that they get initialized initially 
static { 
    reset(); 
} 

// ... 

} 
+0

這太好了。非常感謝你!想想這樣做會花很長時間。我試圖擺脫使用靜態變量 - 我想我會到達那裏,儘管緩慢。 – user5352515

+0

您的解決方案完全按照我的要求工作。主席先生,再次感謝你。 – user5352515

1

當您使用靜態最終字符串時,JVM會在啓動過程中創建這些字符串。 爲了做你想要的東西,使用返回字符串實例的單例模式。然後,reset()方法可以在需要時重置您正在使用的實例。

0

從您的代碼中,您使用的是實例方法reset()read(),所以我假定您將創建Sign的不同實例。

那麼,爲什麼不能讓每個符號對象保持自己的簽名信息,並重構爲這樣的事情代碼:

public class Sign { 

    protected static final String SIGNS_FORMAT = "Welcome to the %s of the %s %s"; 

    private static final String[] BUILDING_TYPES = new String[]{ 
      "Inn", 
      "Inn", 
      "Tavern", 
      "Pub", 
      "Inn" 
    }; 

    private static final String[] VERB_NAMES = new String[] { 
      "Dancing", 
      "Prancing", 
      "Eating", 
      "Jigging", 
      "Digging", 
      "Flogging", 
      "Floating", 
      "Flying", 
      "Laughing", 
      "Smiling", 
      "Drowning", 
      "Golden", 
      "Silver", 
      "Copper", 
      "Farming", 
      "Running", 
      "Sewing", 
      "Black", 
      "White", 
      "Fighting", 
      "Gesticulating" 
    }; 

    private static final String[] NOUN_NAMES = new String[] { 
      "Pony", 
      "Horse", 
      "Griffin", 
      "Dragon", 
      "Wench", 
      "Bastard", 
      "Ogre", 
      "Troll", 
      "Ox", 
      "Cow", 
      "Cock", 
      "Hen", 
      "Ram", 
      "Ewe", 
      "Dog", 
      "Merchant", 
      "Fisherman", 
      "Arborist", 
      "Archer", 
      "Gallbladder", 
      "Pancreas" 
    }; 

    private String currentSign = getNewSign(); 

    public void reset(){ 
     currentSign = getNewSign(); 
    } 

    public void read(int pos) { 
     GameScene.show(new WndMessage(currentSign)); 
     // the `pos` argument is not used? 
    } 

    private String getNewSign(){ 
     return String.format(SIGNS_FORMAT, buildingText(), verbText(), nounText()); 
    } 

    private String buildingText() { 
     return BUILDING_TYPES[Random.Int(0, BUILDING_TYPES.length - 1)]; 
    } 

    private String verbText() { 
     return VERB_NAMES[Random.Int(0, VERB_NAMES.length - 1)]; 
    } 

    private String nounText() { 
     return NOUN_NAMES[Random.Int(0, NOUN_NAMES.length - 1)]; 
    } 
} 

請注意,我不知道什麼Random.Int是,並假定它需要一個包容性範圍Random.Int(lowerBound, upperBound)。 改爲JDK方法應爲java.util.Random.nextInt(bound)

+0

你已經明白了。 Random.Int只是JDK的Random.Int(lowerBound,upperBound)的一個gussied版本。我真的很喜歡你提出的這個想法。我一直很害怕使用這種字符串風格,並且幾乎忘記了使用%s。 – user5352515