我对这个程序有很多问题。我不能让我的字符串比较工作,我有几个无限循环,我不知道如何解决。我会很高兴看到一双新鲜的眼睛向我展示我所缺少的东西,我已经盯着它看了3天而且我一直在跳过我的错误。程序需要从给定的文本文件中选择一个随机单词,对该单词进行加扰,然后让用户猜出原始单词是什么。提示应显示单词中给定随机位置的字母。
import java.util.Scanner;
import java.util.Random;
import java.io.*;
import java.util.*;
public class Proj4 {
public static void main (String[] args) throws IOException{
int wordScore = 10;
int score = 0;
boolean game = true;
int i = 0;
Scanner in = new Scanner(System.in);
char responseLetter = 'n';
boolean allTrue = false;
boolean scramble = true;
StringBuilder sb = new StringBuilder ();
int counter = 0;
int position = 0;
System.out.println("Enter in a file containing words (Ex: words.txt) : ");
String filename = in.nextLine();
Scanner inFile = new Scanner(new File(filename));
String size = inFile.nextLine();
System.out.println(size);
int arrayLength = Integer.parseInt(size);
String[] wordList = new String[arrayLength];
Random rndm = new Random();
while (inFile.hasNext()) {
// this section puts the contents of the text file into the array
wordList[i] = inFile.nextLine();
i++;
}
while (game == true) {
// System.out.println("Current puzzle: " + scrambledWord);
// System.out.println("Current points for word " + wordScore);
// System.out.println("Enter (g)uess, (n)ew word, (h)int, or (q)uit: ");
// responseLetter = in.next().charAt(0);
if (responseLetter == 'n') {
// goes back to the beginning but does not change word. I cant move the picker down becauase it throws off g
String pickedWord = wordList[rndm.nextInt(arrayLength )];
boolean [] used = new boolean[pickedWord.length()] ; // the random word gets picked here
String [] letters = pickedWord.split("");
while (allTrue == false) {
int randomLetter = rndm.nextInt(pickedWord.length());
if (used[randomLetter] != true) {
sb.append(letters[randomLetter].charAt(0)); // does this line work?
used[randomLetter] = true;
} else if (used[randomLetter]== true) {
scramble = true;
counter++;
}
if (counter > (2 * pickedWord.length())) {
allTrue = true;
}
}
String scrambledWord = sb.toString();
if (scrambledWord.equals(pickedWord)) {
System.out.println("The words match");
allTrue = false;
}
System.out.println("Current puzzle: " + scrambledWord);
System.out.println("Current points for word " + wordScore);
System.out.println("Enter (g)uess, (n)ew word, (h)int, or (q)uit: ");
responseLetter = in.next().charAt(0);
} else if (responseLetter == 'g') {
// automatically says guess is wrong. something wrong with equals()?
System.out.println("Enter your guess: ");
in.nextLine();
String guess = in.nextLine();
if (guess.equals(pickedWord)) {
System.out.println("You guessed it!");
score += wordScore;
game = true;
} else if (!guess.equals(pickedWord)) {
System.out.println("Oops! Try again.");
wordScore--;
System.out.println("Current points for word " + wordScore);
System.out.println("Enter (g)uess, (n)ew word, (h)int, or (q)uit: ");
responseLetter = in.next().charAt(0);
}
} else if (responseLetter == 'h') {
//THIS BLOCK WORKS. DONT EVEN LOOK AT IT
Random r = new Random();
int hint = r.nextInt(pickedWord.length());
System.out.println(hint);
char letterAtSpot = pickedWord.charAt(hint);
System.out.println("The letter at spot " + hint + " is " + letterAtSpot);
wordScore--;
} else if (responseLetter == 'q') {
game = false;
} else {
System.out.println("Invalid Choice - 'g', 'n', 'h', 'q' only.");
responseLetter = 'n';
}
}
System.out.println("Goodbye!");
System.out.println("Total score: " + score);
}
}
答案 0 :(得分:0)
突然出现的第一件事是if / else阶梯。
String scrambledWord = sb.toString();
if (scrambledWord.equals(pickedWord)) {
System.out.println("The words match");
allTrue = false;
}
System.out.println("Current puzzle: " + scrambledWord);
System.out.println("Current points for word " + wordScore);
System.out.println("Enter (g)uess, (n)ew word, (h)int, or (q)uit: ");
responseLetter = in.next().charAt(0);
}
注意在获取responseLetter的响应之前如何关闭if语句。这意味着,您的循环条件:“当游戏== true”未正确评估时。
答案 1 :(得分:0)
刚出门,我有一个编译错误,因为pickedWord
被引用在声明它的if语句之外。因此,到if (guess.equals(pickedWord))
时,编译器不知道pickedWord
是什么。