我正在创建一个java程序,并坚持在数组中搜索char的概念。
代码是创建一个刽子手游戏,其中一部分将显示部分完成的刽子手,一个字母和一组空白(“_”),用于需要猜测的单词的长度。在每次猜测之后,我需要取出字母表中猜到的字母(这是我现在正在播放的部分),如果这是一个正确的猜测,用该字母替换单词中的空白或者如果它是错误的猜猜,完成刽子手的另一部分。
我之前只创建了代码来搜索数组中的数字,而不是字符。我一直在浏览我的书和互联网,但我找不到解决方案。
有没有人知道这样做的方法或者我想找到方法的地方?
抱歉,我已经发布了一个关于这项任务的问题,但它将在几天后到期,我正在努力。到目前为止,这是我的代码(我正在努力的部分位于最底层):
int* b = new int(5);
int* c = new decltype(*b)(5);
cout << *c << endl;
答案 0 :(得分:1)
您可以使用contains()
方法查找列表中的字符。从该玩家输入的char c = guessStr.toLowerCase().toCharArray()[0];
通过guessStr
获取字符。并通过if (guessedCharList.contains(new Character(c)))
在列表中进行搜索。 List只能存储对象,因此我们必须通过char
将Character
基元类型转换为对象new Character(c)
。
以下代码将帮助您!但你应该对它进行优化。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;
public class HangMan {
private static List<Character> guessedCharList = new ArrayList<Character>();
private static List<Character> validCharList = new ArrayList<Character>();
private static String rndWord = null;
private static int numberOfWrongGuess = 0;
public static void main(String[] args) {
String[] words = { "javascript", "declaration", "object", "program",
"failing" };
// generate random word from list
Random rnd = new Random();
rndWord = words[rnd.nextInt(words.length)];
System.out.println(rndWord);
// gets length of generated word
char[] displayArray = new char[rndWord.length()];
// displays "_" for number of chars in word
for (int i = 0; i < rndWord.length(); i++) {
displayArray[i] = '_';
}
char[] alphabet = { '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' };
String hangman = null;
boolean finished = false;
do {
hangman = "Let's Play Hangman!!" + "\n" + "-------------" + "\n"
+ "|" + "\n" + "|" + "\n" + "|" + "\n" + "|" + "\n" + "|"
+ "\n" + "|" + "\n" + "|" + "\n" + "|" + "\n" + "\n"
+ Arrays.toString(displayArray) + "\n" + " ";
JOptionPane.showMessageDialog(null,
hangman + " " + Arrays.toString(alphabet) + " ");
guess();
for (int i = 0; i < rndWord.length(); i++) {
if (validCharList.contains(rndWord.charAt(i)))
displayArray[i] = rndWord.charAt(i);
else
displayArray[i] = '_';
}
for (char c : displayArray) {
if (c == '_') {
finished = false;
break;
} else
finished = true;
}
if (numberOfWrongGuess >= 5) { // 5 wrong guess: "he was hanged."
JOptionPane.showMessageDialog(null, "Man was hanged!");
finished = true;
}
} while (!finished);
hangman = "Let's Play Hangman!!" + "\n" + "-------------" + "\n" + "|"
+ "\n" + "|" + "\n" + "|" + "\n" + "|" + "\n" + "|" + "\n"
+ "|" + "\n" + "|" + "\n" + "|" + "\n" + "\n"
+ Arrays.toString(displayArray) + "\n" + " ";
JOptionPane.showMessageDialog(null,
hangman + " " + Arrays.toString(alphabet) + " ");
}
// get letter
public static String guess() {
String guessStr = "";
while (guessStr == null || guessStr.trim().isEmpty()
|| guessStr.length() > 1) {
guessStr = JOptionPane.showInputDialog("Enter a letter to guess: ");
}
char c = guessStr.toLowerCase().toCharArray()[0];
if (guessedCharList.contains(new Character(c))) {
JOptionPane
.showMessageDialog(null, "You already give that answer!");
guess();
}
guessedCharList.add(new Character(c));
if (rndWord.contains(c + "")) {
validCharList.add(new Character(c));
return c + "";
} else {
numberOfWrongGuess++;
return "";
}
}
}
答案 1 :(得分:0)
问题是数组在初始化后无法改变大小。一些解决方案是:
您可以使用List(例如java.utils.ArrayList)。这样您就可以动态搜索和删除条目。
另一种可能性是使用未使用的字符,例如&#39; - &#39;。您可以替换该字符,而不是删除条目。这样您就必须在访问阵列时进行检查。
答案 2 :(得分:-1)
import java.util.HashMap;
import java.util.Map;
public class MatchingWordsInStream {
Map < String, Integer > map;
String input;
char arr[];
int valueArr[];
StringBuffer stringArr[];
int space = 1;
public MatchingWordsInStream(String input) {
this.input = input;
}
public void initialiseCharArray() {
arr = new char[input.length()];
for (int i = 0; i < input.length(); i++) {
arr[i] = input.charAt(i);
if (arr[i] == ' ') space++;
}
}
public void rCharCalculate() {
stringArr = new StringBuffer[space];
valueArr = new int[space];
stringArr[0] = new StringBuffer();
int index = 1;
for (int i = 0; i < input.length(); i++) { //this loop make string array char
ch = arr[i];
if (ch == ' ') {
stringArr[index] = new StringBuffer();
++index;
} else {
stringArr[index - 1] = stringArr[index - 1].append(arr[i]);
}
}
for (int k = 0; k < stringArr.length; k++) {
StringBuffer s = stringArr[k];
for (int j = 0; j < stringArr.length; j++) {
if (s.toString().equals(stringArr[j].toString())) {
valueArr[k] += 1;
}
}
}
}
public void result() {
map = new HashMap < String, Integer > ();
for (int i = 0; i < stringArr.length; i++) {
map.put(stringArr[i].toString(), valueArr[i]);
}
System.out.println(map);
}
public static void main(String[] args) {
MatchingWordsInStream obj = new MatchingWordsInStream("$INDIAN$ wangi bk. wangi bk. 1010 0101 0101 hi hi ii ii ii ii");
obj.initialiseCharArray();
//make string to char array
obj.rCharCalculate();
obj.result();
//just for display
}
}
}