java - 如何以不同的方法从数组列表中选择大小写

标签 java arrays if-statement

我目前正在用java制作一个绞刑吏游戏,我在一种方法中按难度组织了单词列表,然后在另一种方法中,我有一个if语句询问用户想玩哪个难度。我该怎么做呢?我的代码如下:

public static int word(){
 String words[] = new String[26];
    switch(diff){
    case 1:
      words[0] = "cat";
      words[1] = "dog";
      words[2] = "book";          
      words[3] = "breakfeast";          
      words[4] = "telephone";          
      words[5] = "mixture";          
      words[6] = "music";          
      words[7] = "animal";          
      words[8] = "school";          
      words[9] = "plant";          
      words[10] = "pen";          
      words[11] = "pencil";          
      words[12] = "paper";          
      words[13] = "note";          
      words[14] = "fog";          
      words[15] = "smoke";        
      words[16] = "bake";          
      words[17] = "alone";          
      words[18] = "drive";          
      words[19] = "town";          
      words[20] = "city";          
      words[21] = "sunny";          
      words[22] = "shine";          
      words[23] = "polish";          
      words[24] = "cap";          
      words[25] = "hat";

      break;
    case 2:
      words[0] = "president";
      words[1] = "exclamation";          
      words[2] = "statement";          
      words[3] = "television";          
      words[4] = "physics";          
      words[5] = "algebra";          
      words[6] = "geometry";          
      words[7] = "difficult";          
      words[8] = "extreme";          
      words[9] = "procedure";          
      words[10] = "ship";          
      words[11] = "soldier";          
      words[12] = "lunch";          
      words[13] = "hockey";          
      words[14] = "tennis";          
      words[15] = "soccer";          
      words[16] = "football";          
      words[17] = "basketball";          
      words[18] = "bias";          
      words[19] = "magazine";          
      words[20] = "computer";          
      words[21] = "internet";          
      words[22] = "allegedly";          
      words[23] = "system";          
      words[24] = "unison";          
      words[25] = "excited";         
      break;
    case 3:
      words[0] = "amalgamation";          
      words[1] = "proclomation";          
      words[2] = "establishment";          
      words[3] = "rehabilitation";          
      words[4] = "rhinoceros";          
      words[5] = "velociraptor";         
      words[6] = "declaration";         
      words[7] = "announcement";          
      words[8] = "binomial";          
      words[9] = "polynomial";          
      words[10] = "congregation";          
      words[11] = "obligation";          
      words[12] = "structure";          
      words[13] = "description";          
      words[14] = "perscription";          
      words[15] = "subscribe";          
      words[16] = "address";          
      words[17] = "township";          
      words[18] = "mischievous";          
      words[19] = "bewildered";          
      words[20] = "accusation";          
      words[21] = "designation";          
      words[22] = "disgusting";          
      words[23] = "prolonged";          
      words[24] = "restoration";          
      words[25] = "regeneration";          
  }

int i = words.length;

Random rng = new Random();     //This block of code chooses random word from array list
int choice = rng.nextInt(words.length); //Varible storing random word
String wd = words[choice];
out.println(wd);
}



public static int gameStart(){
    Scanner qwe = new Scanner(in);
    out.println("Welcome to my Hang Man game!\n");
    out.println("What difficulty would you like to play on?\t1-3");
    int diff = qwe.nextInt();

    if (diff == 1){
        //not sure what would go here
    }
    else if (diff == 2){
        //not sure what would go here
    }
    else{
        //not sure what would go here
    }


}

最佳答案

  1. 将每个难度的单词存储在单独的文本文件中。
  2. 创建一个方法String[] getWords(intdifficulty),该方法将返回从文件加载的给定难度的单词列表。
  3. 不要在一个方法中混合处理程序不同方面的代码 - 就像在 word() 方法中那样。创建单词列表、选择随机单词并将其显示在屏幕上听起来像是三个不同的方面。这大约意味着 3 种方法。

关于java - 如何以不同的方法从数组列表中选择大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19481129/

相关文章:

java - NetBeans 中表的 Swing Bean 绑定(bind)

java - 如何判断OpenJDK是否安装了调试符号

java - 为什么序列化需要在实现的类中提供序列版本 UID?

c - 如何将 MAC 地址(以字符串形式)转换为整数数组?

python - 如何通过移位数组在 numpy 数组中局部移位值?

bash - 'if -e' 和 'if -f' 之间的区别

java - 如何从嵌套的 Hashmap 中获取数据?

javascript - 递归函数数组到字符串javascript

java - 如果有多个&&,|| java中的条件评估

php - 如何使用三元运算符编写这些 `if..else` 条件语句