I am new to programming and I am not sure what to do next? Any ideas?

import java.util.*; public class HangMan2 { char guess; String userInputs; static Scanner scan = new Scanner(System.in); static Random rand = new Random();

    // The following routine will determin if the character c
    // is inside the String str.  A true is returned if it is inside.

    static boolean isIn(char c, String str)
    {

        if(str.length()==0)
            return false;

        for(int i=0;i<=str.length();i++)

            if (c==str.charAt(i));
        return true;
    }

    // If userInputs contains "ard" and strToGuess contains "aardvark" then
    // the following routine prints out an output that looks something like:
    //
    // Current Status for userInpts=ard
    // a a r d _ a r _

    // This routine returns true if all letters were guessed, otherwise false is returned.


    static boolean printCurrStatus(String strToGuess, String userInputs){


        char guess = userInputs.charAt(0);

           if(isIn(guess, strToGuess)==true){

            strToGuess = "";
            char current;
            for (int i = 0; i < strToGuess.length(); i++)
            {
            current = strToGuess.charAt(i);
            if (current == '_' || current == ' ') 
            {
                strToGuess = strToGuess + "_";
            }
            else
            {
                strToGuess = strToGuess + current;
            }

            }
            return true;

            }
            else 
                return false;
    }




    // The following routine will return a random String from the list of words:
    // elephant, tiger, monkey, baboon, barbeque, giraffe,  simple, zebra, 
    // porcupine, aardvark

    static String getNextWordToGuess()
    {

             Random generator = new Random();
              String[] array  = { "aardvark","porcupine","zebra","simple","giraffe","barbeque","baboon", "monkey","tiger","elephant"};
              int rnd = generator.nextInt(array.length);

              return array[rnd];




        //********** Fill in Details
        // HINT: a switch statement can be quite useful here
    }

    // The following routine plays the hangman game. It calls getNextWordToGuess to
    // get the word that should be guessed.  It then has a loop which outputs the 
    // following prompt:
    // Enter next letter
    //
    // A String named userInputs stores all letters selected already.  
    // Then the routine printCurrStatus is called to print out the current status of
    // the guessed word.  If printCurrStatus returns true, we are done.

    static void playGame()
    {  
        boolean current=false;
        String word = HangMan2.getNextWordToGuess();

    String userInputs;

        do{
            System.out.println("Hello, and welcome to Hangman!");
                System.out.println("Enter next letter");
                userInputs= scan.next(); 
                current= HangMan2.printCurrStatus(word,userInputs);
        }while(printCurrStatus(word, userInputs)==true);

    }

//********** Fill in Details


// main will call playGame to play the hangman game.
// Then main will issue the prompt:
// Do you want to play again (y or n)
// If the answer is "y", then call playGame again, otherwise exit

public static void main(String[] args)
{
    playGame();
    String retry_ans;
      boolean retry= true;


    System.out.println("Would you like to try again?");
    retry_ans = scan.next();
    if(retry_ans.equalsIgnoreCase("yes"))
    {
        retry = true;
        System.out.println("Please enter a new secret word:");
        String newWord = scan.next();
        HangMan2.getNextWordToGuess();

    }
}
}
View Answers









Related Tutorials/Questions & Answers:
Advertisements