gpt4 book ai didi

java - 在数组中搜索字符

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:10 24 4
gpt4 key购买 nike

我正在创建一个java程序,并且一直坚持在数组中搜索字符的概念。

该代码用于创建一个刽子手游戏,其中一部分将显示部分完成的刽子手、一个字母表和一组空格(“_”),用于表示需要猜测的单词的长度。每次猜测后,我需要从字母表中取出猜测的字母(这是我现在正在做的部分),如果猜测正确,则用该字母替换单词中的空白,或者如果猜测错误,则完成刽子手的另一部分。

我之前只创建了代码来搜索数组中的数字,而不是字符。我一直在翻阅我的书和互联网,但找不到解决方案。

没有人知道这样做的方法或者我可以在哪里找到方法吗?

抱歉,我已经发布了有关此作业的问题,但几天后就要到期,我正在努力。

这是到目前为止我的代码(我正在努力解决的部分位于最底部):

public static void main(String[] args)
{
String[] words =
{
"javascript", "declaration", "object", "program", "failing"
};
//generate random word from list
Random rnd = new Random();

String rndWord = words[rnd.nextInt(words.length)];

//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
= "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 char guess()
{
String guessStr = JOptionPane.showInputDialog("Enter a letter to guess: ");

// check if have at least one letter
if (guessStr.length() > 0)
{

}
char guessChar = guessStr.charAt(0);
return guessChar;
}

//remove guess from alphabet
private char[] getCharArray(char[] array)
{
//this is the part I'm not sure about
}

最佳答案

您可以使用contains()方法来查找列表中的字符。通过 char c = GuessStr.toLowerCase().toCharArray()[0]; 从玩家输入的 guessStr 获取字符。并通过 if (guessedCharList.contains(new Character(c))) 在 List 中搜索它。 List只能存储对象,因此我们必须通过new Character(c)char原始类型转换为对象Character

下面的代码将会帮助你!但你应该对其进行优化。

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

关于java - 在数组中搜索字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com