gpt4 book ai didi

java - 在单词争夺游戏的 final方法中从字符串生成器获取返回类型时遇到问题

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

我必须制作一个单词扰乱器“游戏”,并且我拥有需要使用的所有方法。我仍然需要在主方法中添加代码来要求用户猜测并使用 .equals 来查看它是否是正确的单词。那不是问题。我的 scrambleWord(word) 方法遇到问题。我使用了字符串生成器,但不知道如何返回。代码看起来不错并且可以构建,但说有一个错误。有人可以帮忙吗?这是我现在的代码。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
*
* @author Brian2
*/
public class Project3 {

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {

//Shuffle s = new Shuffle();
// s.shuffle("hello");

String[] words = createListOfWords();
String word = chooseRandomWord(words);
String scrambledWord = scrambleWord(word);
System.out.println(scrambledWord);

/* code for asking userers guess
*
*use .equals
*
*
*/

}
double[] wordList = new double[65573];

private static String[] createListOfWords() throws FileNotFoundException, IOException {
BufferedReader in = new BufferedReader(new FileReader("wordlist.txt"));
String str;

List<String> list = new ArrayList<String>();
while ((str = in.readLine()) != null) {
list.add(str);
}

String[] stringArr = list.toArray(new String[0]);

// test to see if array stored--System.out.println(Arrays.toString(stringArr));
return stringArr;

}

/**
*
* @param words
* @return
*/
private static String chooseRandomWord(String[] words) {

int index = new Random().nextInt(words.length);

String word = words[index];

return word;
}

private static String scrambleWord(String word) {

char[] charArray = word.toCharArray();



List<Character> characters = new ArrayList<>();

StringBuilder scrambledWord = new StringBuilder(word.length());

int randPicker = (int) (Math.random() * characters.size());
StringBuilder append = scrambledWord.append(characters.remove(randPicker));




/*for (int i = charArray.length - 1; i > 0; i--) {
int j = ((int)(Math.random())(i + 1));

double scrambledWord = charArray[i];
charArray[i] = charArray[j];
charArray[j] = (char) scrambledWord;
}*/

return scrambledWord.toString() ;
/*This method should take a word as an argument, and return the
same word, except scrambled. In order to scramble a word, convert
it to an array of chars, like this:*/
//char[] charArray = word.toCharArray();

}

}

最佳答案

您从未在 scrambleWord 中的List 中添加任何字符。我会使用 Collections.shuffle(List)和类似的东西

private static String scrambleWord(String word) {
List<Character> characters = new ArrayList<>();
for (char ch : word.toCharArray()) {
characters.add(ch);
}
Collections.shuffle(characters);
StringBuilder sb = new StringBuilder();
for (char ch : characters) {
sb.append(ch);
}
return sb.toString();
}

关于java - 在单词争夺游戏的 final方法中从字符串生成器获取返回类型时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791851/

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