I am new to programming and I am not sure what to do next? Any ideas?
0 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners

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)

            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 + "_";
                strToGuess = strToGuess + current;

            return true;

                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;

            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)
    String retry_ans;
      boolean retry= true;

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



View Answers

Related Tutorials/Questions & Answers: