Advertisement
Guest User

Jotto

a guest
May 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class WordGuess
  4. {
  5.     private char [] word;
  6.  
  7.     public WordGuess()
  8.     {
  9.        
  10.     }
  11.    
  12.     public WordGuess(String w)
  13.     {
  14.         word = new char[w.length()];
  15.    
  16.         for (int i = 0; i < word.length; i++)
  17.             word[i] = w.charAt(i);
  18.     }
  19.    
  20.     public int match(String w)
  21.     {      
  22.         int count = 0;
  23.        
  24.         for (int i = 0; i < word.length; i++)
  25.             if (w.contains("" + word[i]))
  26.                 count++;
  27.         return count;
  28.     }
  29.    
  30.     public int getLength()
  31.     {
  32.         return word.length;
  33.     }
  34.  
  35.     public static void main(String [] mona)
  36.     {
  37.         Scanner scan = new Scanner(System.in);
  38.        
  39.         System.out.println("Welcome to the Word Guessing Game!");
  40.         System.out.print("Make sure all the letters are different. Please enter in your word to guess: ");
  41.        
  42.         String input = scan.next();
  43.  
  44.         WordGuess game = new WordGuess(input);
  45.        
  46.         for (int i = 0; i < 100; i++)
  47.             System.out.println();
  48.        
  49.         System.out.println("Start Guessing!");
  50.         System.out.println("Enter in your guess: ");
  51.         String guess = scan.next();
  52.        
  53.         while (game.match(guess) != game.getLength())
  54.         {
  55.             System.out.println(guess + " and the hidden word have " + game.match(guess) + " letters in common!");
  56.             System.out.println("Enter in your next guess: ");
  57.             guess = scan.next();
  58.         }
  59.        
  60.         System.out.println("Congratulations! You got the word, \"" + guess + "\"!");
  61.        
  62.         scan.close();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement