如何把在多个索引字符 [英] how to put char at multiple indices
问题描述
我要考虑一个12字母的单词和计算机有猜词。
我的信件Z.一个ArrayList我让电脑随机挑选一个信我已经把信的索引,如果这封信是在单词。但是如果信一直走在索引2和8 我怎么能这样做呢?
一个暗示是非常有益的。
i have to think a 12 letter word and the computer has to guess the word.
i have an arraylist of letters A to Z. i let the computer randomly pick a letter and i have to put the letter at an index if the letter is in the word. but what if the letter has to go at index 2 and 8. how could i do that?
a hint would be very helpfull.
!!如果计算机猜测的字字母i设置相应的指数在那封信我例如2型
!! if the computer guesses a letter in the word i set that letter at the corresponding index i type 2 for example
知道对不对,它把信只在一个索引然后删除信(因为它可以客串那封信只有一次。我使用的是12字母的单词。
right know it puts the letter only at one index then removes the letter(because it can gues that letter only one time. i'm using a 12 letter word.
private Scanner sc;
private ArrayList<Character> letters;
private int answer;
private char randL;
private char ch;
private int wrongG = 0;
private int wrongA = 0;
private int randNum;
private StringBuilder hiddenW = new StringBuilder("............");
private boolean match;
public void gameComputer() {
for (int i = 0; i < 26; i++) {
ch = (char) ('A' + i);
letters.add(ch);
}
while (wrongA < 10) {
randNum = (int) (Math.random() * letters.size());
randL = letters.get(randNum);
System.out.println("De computer raadt een " + randL
+ ". Op welke plaats(en) staat die letter?");
answer = sc.nextInt();
for (int ii = 0; ii < hiddenW.length(); ii++) {
if (answer > -1 && answer < 12) {
hiddenW.setCharAt(answer, randL);
match = true;
}
}
if (answer == 100) {
match = false;
}
if (match) {
System.out.println(hiddenW);
System.out.println("Aantal fouten: " + wrongA);
}
if (!match) {
wrongA = wrongA + 1;
System.out.println("Aantal fouten: " + wrongA);
}
if (wrongA == 10) {
System.out.println("you couldn't guess the word");
break;
}
letters.remove(randNum);
}
}
输出:
the computer guessed O. Op welke plaats(en) staat die letter?
2 8
..O.........
Aantal fouten: 0
the computer guessed I. Op welke plaats(en) staat die letter?
..O.....I...
Aantal fouten: 0
the computer guessed K. Op welke plaats(en) staat die letter?
我想将在多个指标的信
就像如果计算机猜到了 A
我想键入2和8,设置字母A
在指数2和8
推荐答案
如果您希望能够给几个指标,而不是一个,可以使用 sc.nextLine()
而不是 sc.nextInt()
键,(如果你鄙视逗号或别的东西),写用逗号分隔的指标 - 如2,8。然后,你可以通过使用读取整个字符串的charAt
,并通过整个字符串或其他任何你喜欢的方式进行迭代。
If you want to be able to give several indices instead of one, use sc.nextLine()
instead of sc.nextInt()
and write the indices separated by a comma (or something else if you despise commas) - for example "2,8". Then you can read the whole string by using charAt
and iterating through the whole string or any other way you like.
这篇关于如何把在多个索引字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!