誰かがこれに答えてくれることを願っているので、これをうまく説明しようとします。
私の目標は、5 つの正方形のライン上に最大 3 つの固有母音 (AEIOU)を生成することです。2D 配列 ( board[][]
) を使用して 25 個の正方形を作成しましたが、最初の行を最初に実行したいと考えています。次のようにイメージしてください。
さて、私の問題は、正方形にランダムな文字を生成しようとすると、最初の文字が表示されないことです。たとえば、E
とは正方形にのみ表示され、 は表示されませO
ん。コンソールでは印刷されますが、GUI では印刷されません。O
E
また、重複した文字が表示されることもあります。これを修正する方法がわかりません :|
これまでに行ったコードは次のとおりです。
String board[][] = new String[5][5];
String alphabet = "AEIOU";
int numArray[] = new int[5]; //where I can store random indices of alphabet
int finalIndex = 0;
int random = (int) (Math.random()*3) + 1; //random number of vowels to be generated
//this loop covers everything
for(int ctr = 0; ctr < random; ctr++) {
while(ctr != finalIndex) { //checks if there are any duplicates
int rand = (int) (Math.random()*4); //random position for the letter
numArray[ctr] = rand;
while(numArray[ctr] != numArray[finalIndex]) {
finalIndex++;
}
}
//finds the position of the letter in alphabet and converts it to String
char character = alphabet.charAt(numArray[ctr]);
String s = String.valueOf(character);
System.out.println(s);
//loop for putting the letters to the 2D array
for(int i = 0; i < board.length; i++) {
int gen = (int) (Math.random()*4); //random square for letter
for(int j = 0; j <= gen; j++) {
if(i == 0 && j < 5) { //row 1
board[i][gen] = s;
}
}
}
}
物事を単純にするためだけに、GUI コードをもう配置しないことにしました。