0

我不知道如何让程序打印出像足球这样的单词中的双字母。每当随机生成器选择足球时,我猜系统会打印出字母“c”••c•••。我希望它打印出来••cc••。任何帮助将不胜感激

import TurtleGraphics.*;
import java.util.Scanner;
import java.util.Random;

public class HangmanGame
{
    public static void main(String [] args)
    {
        String repeat = "yes";
    while((repeat.equals("yes"))||(repeat.equals ("Yes")))
    {
        SketchPadWindow pad = new SketchPadWindow(800, 800);
        StandardPen pen = new StandardPen(pad);
        Scanner scan = new Scanner(System.in);
        HangmanTest hang = new HangmanTest();
        Random randomGenerator = new Random();


        //Ints
        int index=0;
        int number=0;
        int counter=0;
        int randomNum = 0;


        //Strings
        String Secretword = "";
        String choose = "";
        String guessLetter = "";
        String guesses = "";
        String diff = "";
        String Pokemon [] = {"pikachu", "jigglypuff", "dugtrio", "muk", "ditto", "eevee", "mewtwo", "cyndaquil", "raikou"};
        String Sports [] = {"tennis", "rowing", "golf", "lacrosse", "sailing", "soccer", "football", "baseball", "volleyball"};
        String Periodic [] = {"iron", "carbon", "radon", "ununcodium", "oxygen", "magnesium", "antimoney", "iodine", "cadmium"};
        String Food [] = {"pizza", "spaghetti", "muffin", "bagel", "chicken", "steak", "apple", "chips", "cookies"};
        String Boardgames [] = {"life", "monopoly", "battleship", "boggle", "sorry", "operation", "blokus", "cranium", "gab"};
        String Planets [] = {"mars", "pluto", "saturn", "earth", "jupiter", "uranus", "neptune", "mercury", "venus"};
        String States [] = {"utah", "washington", "colorado", "california", "texas", "wyoming", "missouri", "kentucky", "connecticut"};
        String Spanish [] = {"hola", "adios", "feliz", "durante", "conocer", "sobre", "decir", "trabajar", "manzana"};
        String ACT [] = {"congregation", "camaraderie", "digression", "emulate", "fortuitous", "frugal", "evanescent", "hypothesis", "extenuating"};
        String dashes = "";
        hang.drawGallo(pen);

        System.out.println("There are three difficulties to chose from with sub categories in each");
        System.out.println("They are: Easy, Medium and Hard");
        diff = scan.nextLine();

        if(diff.equals("Easy")){
        System.out.println("Please choose one of the three following categories:");
        System.out.println("Sports, Food or Boardgames");
        choose = scan.nextLine();
            if(choose.equals("Sports")){
                randomNum = randomGenerator.nextInt(Sports.length-1);
                Secretword = Sports[randomNum];
            }else if (choose.equals("Food")){
                randomNum = randomGenerator.nextInt(Food.length-1);
                Secretword = Food[randomNum];
            }else if (choose.equals("Boardgames")){
                randomNum = randomGenerator.nextInt(Boardgames.length-1);
                Secretword = Boardgames[randomNum];
            }   

        }else if (diff.equals("Medium")){
        System.out.println("Please choose one of the three following categories:");
        System.out.println("Pokemon, Planets or States");
        choose = scan.nextLine();
            if(choose.equals("Pokemon")){
                randomNum = randomGenerator.nextInt(Pokemon.length-1);
                Secretword = Pokemon[randomNum];
            }else if(choose.equals("Planets")){
                randomNum = randomGenerator.nextInt(Planets.length-1);
                Secretword = Planets[randomNum];
            }else if(choose.equals("States")){
                randomNum = randomGenerator.nextInt(States.length-1);
                Secretword = States[randomNum];
            }

        }else if (diff.equals ("Hard")){
        System.out.println("Please choose one of the three following categories:");
        System.out.println("Periodic, Spanish or ACT");
        choose = scan.nextLine();
            if(choose.equals("Periodic")){
                randomNum = randomGenerator.nextInt(Periodic.length-1);
                Secretword = Periodic[randomNum];
            }else if(choose.equals("Spanish")){
                randomNum = randomGenerator.nextInt(Spanish.length-1);
                Secretword = Periodic[randomNum];
            }else if(choose.equals("ACT")){
                randomNum = randomGenerator.nextInt(ACT.length-1);
                Secretword = ACT[randomNum];
            }
        }

        for(int x = 0; x< Secretword.length(); x++)
        dashes += "*";
        System.out.println(dashes);

        while(number <= 9 && !dashes.equals(Secretword) )
        {
            String letter = "";
            System.out.println("Please enter a letter or the entire word.");
            letter = scan.nextLine();
            if(letter.equals(Secretword))
            {
                System.out.println("You correctly guessed the word. Good job!");
                break;
            }
            if(Secretword.indexOf(letter) != -1)
            {
                index = Secretword.indexOf(letter);
                /*for (int x=0; x <Secretword.length() - 1; x++)
                {
                    if (Secretword.charAt(x) == Secretword.charAt(x +1))
                    {
                        System.out.println("Word contains double");
                    }else
                    {
                        System.out.println("Normal word found");
                    }
                }
                */
                System.out.println("You entered a letter in the word");
                dashes = dashes.substring(0, index) + letter + dashes.substring(index +1);
                System.out.println(dashes);
            }
            else 
            {
                System.out.println("You entered an incorrect letter");
                number++;
            }


            if(number == 1)
                hang.drawHead(pen); 
            if(number == 2)
                {
                hang.drawReye(pen);
                hang.drawLeye(pen);
                hang.drawPupils(pen);
                }
            if(number == 3)
                {
                hang.drawNose(pen); 
                hang.drawMouth1(pen);
                }   
            if(number == 4) 
                hang.drawTeeth(pen);
            if(number == 5) 
                hang.drawHair(pen);
            if(number == 6) 
                hang.drawLglasses(pen);
            if(number == 7) 
                hang.drawBody(pen);
            if(number == 8)
                {
                hang.drawRarm(pen); 
                hang.drawLarm(pen);
                }   
            if(number == 9)
                {   
                hang.drawLleg(pen); 
                hang.drawRleg(pen);
                }

        }
        if(number == 10){
            System.out.println("You lose");
            System.out.println("The word was:" + " " + Secretword);
        }else 
        {
                System.out.println("You win");
        }
        System.out.println("Game Over");


        System.out.println("Would you like to play again?");
        repeat = scan.nextLine();


    }//end over all while loop


        System.exit(0);




    }//close main function 

}//close class
4

1 回答 1

0

我看到你试图修复它,所以你知道问题出在哪里。让它工作的最简单的方法是:

        if(Secretword.indexOf(letter) != -1)
        {
            System.out.println("You entered a letter in the word");

            while(Secretword.indexOf(letter) != -1)
            {
                index = Secretword.indexOf(letter);
                dashes = dashes.substring(0, index) + letter + dashes.substring(index +1);
            }

            System.out.println(dashes);
        }
        else 
        {
            System.out.println("You entered an incorrect letter");
            number++;
        }

直到有人进入*它才会冻结。我会把它留给你解决;)

于 2014-05-20T16:30:34.690 回答