Don't like ads? PRO users don't see any ads ;-)
Guest

hangman java

By: a guest on Apr 18th, 2012  |  syntax: None  |  size: 2.23 KB  |  hits: 56  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.Scanner;
  2.  
  3. public class hangman1 {
  4.   //A method to create a substring, in order to show the correct letters such as "Me***n" for the word "Merlin".
  5.   public static String replaceCharAt(String s, int pos, char c) {
  6. return s.substring(0,pos) + c + s.substring(pos+1);
  7. }
  8.   public static void main(String[] args) {
  9.    
  10.  
  11.    
  12.     Scanner input = new Scanner(System.in);
  13.     String word = "";
  14.     String display = "";
  15.     String guess = "";
  16.     int wordLen=0;
  17.     int guessLen=0;
  18.     boolean correct = false;
  19.     boolean win = false;
  20.    
  21.    
  22.    
  23.     //Generating a random number between 1-7
  24.     int randomNumber=((int)(Math.random()*7+1));
  25.    
  26.     switch (randomNumber) {
  27.       case 1 : word = "Merlin";
  28.       case 2 : word = "Dexter";
  29.       case 3 : word = "The Closer";
  30.       case 4 : word = "House";
  31.       case 5 : word = "Friends";
  32.       case 6 : word = "Survivor";
  33.       case 7 : word = "Simpsons";
  34.     }
  35.    
  36.      
  37.      
  38.     //A for loop for counting the attempts
  39.     for (int attempt=9; attempt!=0; attempt--){
  40.    
  41.     //Making sure that the user entered only one letter
  42.     do{
  43.     System.out.println("Please enter a letter: ");
  44.       guess = input.next();
  45.       if (guess.length()>1)
  46.       System.out.println("You have to enter a letter not a word.");
  47.     }while(guess.length()!=1);
  48.      
  49.       wordLen=word.length();
  50.       for(int a=1; a!=wordLen; a++)
  51.         display+="*";
  52.        
  53.       //Another for loop to check every letter in the "word", using the replaceCharAt method created at the top.
  54.     for(int i = 0; i!=(wordLen-1); i++){
  55.      
  56.       if (guess.charAt(0)==word.charAt(i)){
  57.       replaceCharAt(display,i, word.charAt(i));
  58.       attempt++; //So the user dont lose a attempt point when their guess was correct.
  59.       correct = true;
  60.       }
  61.     }
  62.    
  63.     if (correct){
  64.       System.out.println("You found a letter!");
  65.       System.out.println(display);}
  66.     else {
  67.       System.out.println("Your guess was wrong. " + attempt + " attemps left");
  68.       System.out.println(display);}
  69.     }
  70.  
  71.      
  72.        
  73.      
  74.          
  75.          
  76.      
  77.      
  78.      
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.    
  92.  
  93.      
  94.      
  95. }
  96. }