简体   繁体   English

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

[英]Java: How to check the random letters from a-z, out of 10 letters minimum 2 letter should be a vowels

I am writing a program to validate the following scenarios: 我正在编写一个程序来验证以下情况:

Scenario 1: 方案1:

I am using the Random class from java.util. 我正在使用java.util中的Random类。 The random class will generate 10 letters from az and within 10 letter, minimum 2 letters must be a vowels. 随机类将从az生成10个字母,并且在10个字母内,至少2个字母必须是元音。

Scenario 2: 方案2:

When the player 1 and player 2 form a word from AZ, he will score some points. 当玩家1和玩家2组成AZ单词时,他会得分。 There will be a score for each letter. 每个字母都会有一个分数。 I have already assigned the values for AZ. 我已经分配了AZ的值。 At the end of the game, the system should display a scores for player 1 and player 2. How do i do it? 在游戏结束时,系统应显示玩家1和玩家2的得分。我该怎么做?

Please help. 请帮忙。 I will post my code here. 我将在这里发布我的代码。

Thanks a lot. 非常感谢。

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

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])){

  }

   }





  }
 }}

For Scenario 1, IMO the simplest solution would be to generate the 10 chars, count the number of vowels, and if it is less than 2, regenerate the whole set. 对于方案1,IMO最简单的解决方案是生成10个字符,计算元音数量,如果小于2,则重新生成整个集合。 This would make the result look more "random", in the sense that the vowels can be anywhere within the sequence, and there can be more than 2 of them. 在某种意义上,元音可以在序列中的任何位置,并且可以有两个以上,这会使结果看起来更加“随机”。

Btw here 顺便说一句

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

the word list is used to check that the word entered by the actual player is a valid word (ie it exists in a dictionary). 单词列表用于检查实际玩家输入的单词是否是有效单词(即,它存在于词典中)。

Once the word is validated, just iterate through its characters, find their index within the alphabet, then sum up their values. 验证单词后,只需遍历其字符,在字母表中找到其索引,然后对其值求和即可。 For this, you would be better off using a char[] instead of String[] . 为此,最好使用char[]而不是String[]

Update: code example for generating and validating the characters: 更新:用于生成和验证字符的代码示例:

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 + " ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java中文本文件中字母(az)的相对频率计数 - Relative Frequency Count of letters (a-z) from a text file in Java 如何从特定字母列表中获取随机字母? - How to get a random letter from a list of specific letters? Java-如何在不使用!@#$%^&*()的情况下从0打印到z?&gt; &lt;“:{} |字母,仅包括字母和数字 - Java - How to print from 0 to z without !@#$%^&*()?><":{}| letters, only alphabet and numbers 在 Java 中,您如何随机选择一个字母 (az)? - In Java how do you randomly select a letter (a-z)? 使用Java流将字母从A打印到Z. - Printing the letters from A to Z using a Java stream 如果三重奏中的字母重复,如何用字母表中的随机字符替换三重奏中的随机字母? - How to replace a random letter in a triple with a random character from the alphabet, if the letters in the triple are repeated? 如何在jstl中列出az中的每个字母 - How do you list every letter from a-z in jstl Java - 编码字符串/将随机(未使用)字母分配给特定字母 - Java - Encoding a string / Assigning a random (unused) letter to specific letters 如何获取给定值上的匹配字符串(特殊字符(如+-=。etc)或字符串(az字母)。 - how to get the match strings(either special characters(like +-=.etc) or string(a-z letters) on the given values. 如何总结Java中的随机字母数组? - How to sum up array of random letters in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM