Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class WordGuess
- {
- private char [] word;
- public WordGuess()
- {
- }
- public WordGuess(String w)
- {
- word = new char[w.length()];
- for (int i = 0; i < word.length; i++)
- word[i] = w.charAt(i);
- }
- public int match(String w)
- {
- int count = 0;
- for (int i = 0; i < word.length; i++)
- if (w.contains("" + word[i]))
- count++;
- return count;
- }
- public int getLength()
- {
- return word.length;
- }
- public static void main(String [] mona)
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("Welcome to the Word Guessing Game!");
- System.out.print("Make sure all the letters are different. Please enter in your word to guess: ");
- String input = scan.next();
- WordGuess game = new WordGuess(input);
- for (int i = 0; i < 100; i++)
- System.out.println();
- System.out.println("Start Guessing!");
- System.out.println("Enter in your guess: ");
- String guess = scan.next();
- while (game.match(guess) != game.getLength())
- {
- System.out.println(guess + " and the hidden word have " + game.match(guess) + " letters in common!");
- System.out.println("Enter in your next guess: ");
- guess = scan.next();
- }
- System.out.println("Congratulations! You got the word, \"" + guess + "\"!");
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement