Java Notes
Example: Insult Generator using Arrays
Use arrays and method to generate random insults, eg,
Never enter this kind of meaningless garbage again!!!! Never enter this kind of idiotic garbage again!!!! What kind of revolting garbage is this, you hopeless moron? Never enter this kind of idiotic swill again!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// File : data-arrays/insult/PhraseGenerator.java // Purpose: Generates random insults, using arrays of words. // Author : Fred Swartz, 2008 Feb 29, Placed in public domain. package insulter; public class PhraseGenerator { //================================================================ constants private static final String[] NOUN = {"nonsense", "crap", "swill", "garbage"}; private static final String[] PERSON = {"idiot", "imbecile", "moron"}; private static final String[] PERSON_ADJ = {"clueless", "witless", "stupid", "hopeless"}; private static String[] INFO_ADJ = {"revolting", "insulting", "meaningless", "useless", "idiotic"}; //=========================================================== badInputInsult public static String generateBadInputInsult() { String insult; //... Choose one of two sentence templates randomly. switch ((int) (Math.random() * 2)) { case 0: insult = "What kind of " + choose(INFO_ADJ) + " " + choose(NOUN) + " is this, you " + choose(PERSON_ADJ) + " " + choose(PERSON) + "?"; break; case 1: insult = "Never enter this kind of " + choose(INFO_ADJ) + " " + choose(NOUN) + " again!!!!"; break; default: insult = "Oops -- Internal error in generateBadInputInsult()."; } return insult; } //=================================================================== choose // Utility method to choose random element from an array. private static String choose(String[] words) { int randomIndex = (int) (Math.random() * words.length); return words[randomIndex]; // Return random element of array. } } |