Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我无法输出用户';这是一个绞刑游戏中的猜测量_Java_Loops - Fatal编程技术网

Java I';我无法输出用户';这是一个绞刑游戏中的猜测量

Java I';我无法输出用户';这是一个绞刑游戏中的猜测量,java,loops,Java,Loops,这基本上只是我的类的一个hangman项目,我不允许使用数组。该单词对用户隐藏为破折号。用户必须选择一个字母和要检查的空格数。根据用户选择的难度,用户有一定的猜测量。如果他们猜测的字母至少在他们选择的一个空格中,则用户不会失去猜测,如果他们猜测的字母不在他们选择的任何空格中,则用户会失去猜测 例如,输出应该如下所示 秘密词语是:loops 这个词是:-- 请输入您要猜测的字母 k 请输入要检查的空格(用空格分隔) 01 2 3 在您提供的空格中找不到您的信件 剩余猜测数:14 但现在看起来是这样

这基本上只是我的类的一个hangman项目,我不允许使用数组。该单词对用户隐藏为破折号。用户必须选择一个字母和要检查的空格数。根据用户选择的难度,用户有一定的猜测量。如果他们猜测的字母至少在他们选择的一个空格中,则用户不会失去猜测,如果他们猜测的字母不在他们选择的任何空格中,则用户会失去猜测

例如,输出应该如下所示

秘密词语是:loops

这个词是:--

请输入您要猜测的字母

k

请输入要检查的空格(用空格分隔)

01 2 3

在您提供的空格中找不到您的信件

剩余猜测数:14

但现在看起来是这样的

秘密词语是:loops

这个词是:--

请输入您要猜测的字母

k

请输入要检查的空格(用空格分隔)

01 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);
        }
    }
} 






}
e包;
导入java.util.Scanner;
公共级绞刑架{
私有静态最终布尔测试模式=真;
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
键盘。使用分隔符(\\n“);
while(true){
输入你的难度:简单(e)、中等(i)或困难(h);
String diff=keyboard.next();
int amountOfSpaces=0;
字符串响应=”;
字符串猜测=”;
字符串newGuess=“”;
字符串letterInput=“”;
整数计数=0;
字符串newWord=“loops”;//RandomWord.newWord();
整数猜测=0;
for(int i=0;i                      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);
        }
    }
}

}