这基本上只是我班级的一个刽子手项目,我不允许使用数组。该单词用破折号隐藏给用户。用户必须选择一个字母和他们想要检查的空格数。用户根据他们选择的难度有一定的猜测量。如果他们猜到的字母至少在他们选择的一个空格中,则用户不会猜到,如果他们猜到的字母不在他们选择的空格中,那么用户就会失去猜测。
例如输出应该看起来像
秘密词是:循环
这个词是:-----
请输入您要猜的字母
ķ
请输入您要检查的空格(以空格分隔)
0 1 2 3
您提供的空格中未找到您的来信
猜测还剩:14
但它目前看起来像这样
秘密词是:循环
这个词是:-----
请输入您要猜的字母
ķ
请输入您要检查的空格(以空格分隔)
0 1 2 3
您提供的空格中未找到您的来信
猜测还剩:14
您提供的空格中未找到您的来信
猜测还剩:13
您提供的空格中未找到您的来信
猜测剩余:12
您提供的空格中未找到您的来信
猜测还剩:11
这是我到目前为止所拥有的
package e;
import java.util.Scanner;
public class HangmanBeta{
private static final boolean testingMode = true;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
keyboard.useDelimiter("\\n");
while (true) {
System.out.println("Enter your difficulty: Easy (e), Intermediate (i), or Hard (h)");
String diff = keyboard.next();
int amountOfSpaces = 0;
String response = "";
String guess = "";
String newGuess = "";
String letterInput = "";
int count = 0;
String newWord = "loops";//RandomWord.newWord();
int guesses = 0;
for (int i = 0; i < newWord.length(); i++) {
guess = newWord.replaceAll("[^#]", "-");
}
if ((diff.equalsIgnoreCase("e")) || (diff.equalsIgnoreCase("i")) || (diff.equalsIgnoreCase("h"))) {
if (diff.equalsIgnoreCase("e"))
{
guesses = 15;
}
if(diff.equalsIgnoreCase("i"))
{
guesses = 12;
}
if(diff.equalsIgnoreCase("h"))
{
guesses = 15;
}
if (testingMode == true)
{
System.out.println("The secret word is:" + " " + newWord);
}
System.out.println("The word is:" + " " + guess);
while(!newWord.equalsIgnoreCase(guess))
innerloop:
{ while(true)
{
System.out.println("Please enter the letter you want to guess");
letterInput = keyboard.next();
letterInput = Character.toString(letterInput.charAt(0));
if(!Character.isLetter(letterInput.charAt(0)))
{
System.out.println("Your input is not valid, try again");
break;
}
if(letterInput.equalsIgnoreCase("solve"))
{
System.out.println("Please solve the answer:");
String userSolve = keyboard.next();
if (!userSolve.equalsIgnoreCase(newWord))
{
System.out.println("That is not the secret word");
guesses = guesses - 1;
System.out.println("Guesses remaining: " + guesses);
}
else
{
System.out.println("You win!");
System.out.println("You have guessed the word! Congratulations");
System.out.println("Would you like to play again? Yes(y) or No (n)");
response = keyboard.next();
if (response.equalsIgnoreCase("n"))
{
System.exit(0);
}
if(response.equalsIgnoreCase("y"))
{
continue;
}
}
}
System.out.println("Please enter the spaces you want to check (separated by spaces)");
String spaces = keyboard.next();
amountOfSpaces = spaces.length();
if (diff.equalsIgnoreCase("e"))
{
if(amountOfSpaces != 7)
{
System.out.println("Your input is not valid, try again");
break innerloop;
}
}
if (diff.equalsIgnoreCase("i"))
{
if(amountOfSpaces != 5)
{
System.out.println("Your input is not valid, try again");
break innerloop;
}
}
if (diff.equalsIgnoreCase("h"))
{
if(amountOfSpaces != 3)
{
System.out.println("Your input is not valid, try again");
break innerloop;
}
}
for ( String a : spaces.split("\\s"))
{
int x = Integer.valueOf(a);
if (x > guess.length())
{
System.out.println("Your input is not valid, try again");
break innerloop;
}
if (Character.toLowerCase(newWord.charAt(x)) == letterInput.charAt(0))
{
//System.out.println("Guess is correct for position " + x);
guess = guess.substring(0, x) + letterInput + guess.substring(x + 1, guess.length());
}
if (Character.toLowerCase(newWord.charAt(x)) != letterInput.charAt(0))
{
guesses= guesses - 1;
System.out.println("Your letter was not found in spaces you provided");
System.out.println("Guesses Remaining: " + guesses);
}
if (guesses == 0)
{
System.out.println("You have failed to guess the word....:(");
System.out.print("Would you like to play again? Yes(y) or No(n)");
response = keyboard.next();
if (response.equalsIgnoreCase("n"))
{
System.exit(0);
}
if(response.equalsIgnoreCase("y"))
{
continue;
}
}
}
}
if (newWord.equalsIgnoreCase(guess))
{
System.out.println("You win!");
System.out.println("You have guessed the word! Congratulations");
System.out.println("Would you like to play again? Yes(y) or No (n)");
response = keyboard.next();
if (response.equalsIgnoreCase("n"))
{
System.exit(0);
}
if (response.equalsIgnoreCase("y"))
{
continue;
}
}
}
System.out.println("Your Guess is in the word");
}
if(guesses == guesses - 1)
{
//System.out.print(spaces.split("\\s").length);
//System.out.println("Your Guess is in the word");
//System.out.println();
//System.out.println("Updated word " + guess);
//System.out.println("Guesses Remaining: " + guesses);
}
}
}
}
在我使用的例子中,难度设置为简单。
答案 0 :(得分:0)
这里有一个错误:
if (Character.toLowerCase(newWord.charAt(x)) != letterInput.charAt(0))
{
guesses= guesses - 1;
System.out.println("Your letter was not found in spaces you provided");
System.out.println("Guesses Remaining: " + guesses);
}
您无法直接打印邮件,因为您需要确保所有空格都不匹配。这应该是有效的:
public class HangmanBeta {
private static final boolean testingMode = true;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
keyboard.useDelimiter("\\n");
while (true) {
System.out.println("Enter your difficulty: Easy (e), Intermediate (i), or Hard (h)");
String diff = keyboard.next();
int amountOfSpaces = 0;
String response = "";
String guess = "";
String newGuess = "";
String letterInput = "";
int count = 0;
String newWord = "loops";//RandomWord.newWord();
int guesses = 0;
for (int i = 0; i < newWord.length(); i++) {
guess = newWord.replaceAll("[^#]", "-");
}
if ((diff.equalsIgnoreCase("e")) || (diff.equalsIgnoreCase("i")) || (diff.equalsIgnoreCase("h"))) {
if (diff.equalsIgnoreCase("e")) {
guesses = 15;
}
if (diff.equalsIgnoreCase("i")) {
guesses = 12;
}
if (diff.equalsIgnoreCase("h")) {
guesses = 15;
}
if (testingMode == true) {
System.out.println("The secret word is:" + " " + newWord);
}
System.out.println("The word is:" + " " + guess);
while (!newWord.equalsIgnoreCase(guess))
innerloop:
{
while (true) {
System.out.println("Please enter the letter you want to guess");
letterInput = keyboard.next();
letterInput = Character.toString(letterInput.charAt(0));
if (!Character.isLetter(letterInput.charAt(0))) {
System.out.println("Your input is not valid, try again");
break;
}
if (letterInput.equalsIgnoreCase("solve")) {
System.out.println("Please solve the answer:");
String userSolve = keyboard.next();
if (!userSolve.equalsIgnoreCase(newWord)) {
System.out.println("That is not the secret word");
guesses = guesses - 1;
System.out.println("Guesses remaining: " + guesses);
} else {
System.out.println("You win!");
System.out.println("You have guessed the word! Congratulations");
System.out.println("Would you like to play again? Yes(y) or No (n)");
response = keyboard.next();
if (response.equalsIgnoreCase("n")) {
System.exit(0);
}
if (response.equalsIgnoreCase("y")) {
continue;
}
}
}
System.out.println("Please enter the spaces you want to check (separated by spaces)");
String spaces = keyboard.next();
amountOfSpaces = spaces.length();
if (diff.equalsIgnoreCase("e")) {
if (amountOfSpaces != 7) {
System.out.println("Your input is not valid, try again");
break innerloop;
}
}
if (diff.equalsIgnoreCase("i")) {
if (amountOfSpaces != 5) {
System.out.println("Your input is not valid, try again");
break innerloop;
}
}
if (diff.equalsIgnoreCase("h")) {
if (amountOfSpaces != 3) {
System.out.println("Your input is not valid, try again");
break innerloop;
}
}
int numSpacesLeft = spaces.split("\\s").length;
for (String a : spaces.split("\\s")) {
int x = Integer.valueOf(a);
if (x > guess.length()) {
System.out.println("Your input is not valid, try again");
break innerloop;
}
if (Character.toLowerCase(newWord.charAt(x)) == letterInput.charAt(0)) {
//System.out.println("Guess is correct for position " + x);
guess = guess.substring(0, x) + letterInput + guess.substring(x + 1, guess.length());
}
numSpacesLeft--;
if (Character.toLowerCase(newWord.charAt(x)) != letterInput.charAt(0)) {
if (numSpacesLeft == 0) {
guesses = guesses - 1;
System.out.println("Your letter was not found in spaces you provided");
System.out.println("Guesses Remaining: " + guesses);
}
}
if (guesses == 0) {
System.out.println("You have failed to guess the word....:(");
System.out.print("Would you like to play again? Yes(y) or No(n)");
response = keyboard.next();
if (response.equalsIgnoreCase("n")) {
System.exit(0);
}
if (response.equalsIgnoreCase("y")) {
continue;
}
}
}
}
if (newWord.equalsIgnoreCase(guess)) {
System.out.println("You win!");
System.out.println("You have guessed the word! Congratulations");
System.out.println("Would you like to play again? Yes(y) or No (n)");
response = keyboard.next();
if (response.equalsIgnoreCase("n")) {
System.exit(0);
}
if (response.equalsIgnoreCase("y")) {
continue;
}
}
}
System.out.println("Your Guess is in the word");
}
if (guesses == guesses - 1) {
//System.out.print(spaces.split("\\s").length);
//System.out.println("Your Guess is in the word");
//System.out.println();
//System.out.println("Updated word " + guess);
//System.out.println("Guesses Remaining: " + guesses);
}
}
}
}
跳这可以帮到你!