Java:如何检查来自a-z的随机字母,10个字母中最少2个字母应该是元音

时间:2010-06-11 10:02:57

标签: java text random

我正在编写一个程序来验证以下场景:

场景1:

我正在使用java.util中的Random类。随机类将从a-z生成10个字母,在10个字母内,最少2个字母必须是元音。

场景2:

当玩家1和玩家2从A-Z形成一个单词时,他将获得一些积分。每封信都会有一个分数。我已经为A-Z指定了值。在游戏结束时,系统应显示玩家1和玩家2的分数。我该怎么做?

请帮忙。我会在这里发布我的代码。

非常感谢。

===========================================

import java.util.Random;
import java.util.Scanner;

public class FindYourWords {

 public static void main(String[] args) {
  Random rand = new Random();
  Scanner userInput = new Scanner(System.in);

  //==================Player object===============================================
  Player playerOne = new Player();
  playerOne.wordScore = 0;
  playerOne.choice = "blah";
  playerOne.turn = true;

  Player playerTwo = new Player();
  playerTwo.wordScore = 0;
  playerTwo.choice = "blah";
  playerTwo.turn = false;

  //================== Alphabet ==================================================
  String[] newChars = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
    "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
  }; //values of the 26 alphabets to be used

  int [] letterScore = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; // to assign score to the player1 and player 2

  String[] vowel = { "a", "e", "i", "o", "u" };  // values for vowels
  int vow=0;

  System.out.println("FINDYOURWORDS\n");

  int[] arrayRandom = new int[10]; //int array for word limiter
  String[] randomLetter = new String[10]; //storing the letters in newChars into this array

  //===============================================================================
boolean cont = true;

  while (cont) {

   if (playerOne.turn) {
    System.out.print("Letters of Player 1: ");
   }
   else if (!playerOne.turn) {
    System.out.print("Letters of Player 2: ");
   }

   for (int i = 0; i < arrayRandom.length; i++) { //running through the array limiter
   int r = rand.nextInt(newChars.length); //assigning random nums to the array of letters
   randomLetter[i] = newChars[r];
   System.out.print(randomLetter[i]+ " ");

   }

   //input section for player

   System.out.println("");
   System.out.println("Enter your word (or '@' to pass or '!' to quit): ");
   if (playerOne.turn) {
    playerOne.choice = userInput.next();
    System.out.println(playerOne.turn);
    playerOne.turn = false;
   }
   else if (!playerOne.turn){
    playerTwo.choice = userInput.next();
    System.out.println(playerOne.turn);
    playerOne.turn = true;
   }
   //System.out.println(choice);


   String[] wordList = FileUtil.readDictFromFile("words.txt"); //Still dunno what this is for


   if (playerOne.choice.equals("@")) {
    playerOne.turn = false;
   }
   else if (playerTwo.choice.equals("@")) {
    playerOne.turn = true;
   }
   else if (playerOne.choice.equals("!")) {
    cont = false;
   }


   for (int i = 0; i < wordList.length; i++) {
   //System.out.println(wordList[i]);
    if (playerOne.choice.equalsIgnoreCase(wordList[i]) || playerTwo.choice.equalsIgnoreCase(wordList[i])){

  }

   }





  }
 }}

1 个答案:

答案 0 :(得分:1)

对于场景1,IMO最简单的解决方案是生成10个字符,计算元音的数量,如果它小于2,则重新生成整个集合。这会使结果看起来更“随机”,因为元音可以在序列中的任何位置,并且可以有超过2个。

顺便说一句

String[] wordList = FileUtil.readDictFromFile("words.txt"); //Still dunno what this is for

单词列表用于检查实际播放器输入的单词是否为有效单词(即它存在于字典中)。

一旦单词被验证,只需遍历其字符,在字母表中找到它们的索引,然后总结它们的值。为此,最好使用char[]代替String[]

更新:生成和验证字符的代码示例:

final char[] alphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
final Set<Character> vowels = new HashSet<Character>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
char[] randomLetters = new char[10];
int vowelCount;

do {
    for (int i = 0; i < randomLetters.length; i++) {
        int r = rand.nextInt(alphabet.length);
        randomLetters[i] = alphabet[r];
    }
    vowelCount = 0;
    for (char actualChar: randomLetters) {
        if (vowels.contains(actualChar))
            vowelCount++;
    }
} while (vowelCount < 2);
for (char actualChar: randomLetters) {
    System.out.print(actualChar + " ");
}