⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hangman06.java

📁 用java编写的一个小游戏Hangman。可自己设定一个字母
💻 JAVA
字号:
//**************************************************************************
//  Hangman06.java   
// 
//  Author:Frank   (Hui Feng)
// 
//  Version:  Nov.6th.2006
//
//  A two person game of hangman
//**************************************************************************






import pps.Keyboard;

public class Hangman06                                 // Game Hangman

  {
    public static void main(String[]args)
      { int time=0;
      
         String userinput;
           
  //-------------------------------------------------------------------------
  //Begin the game
  //-------------------------------------------------------------------------
         System.out.print("Do you want to paly a game of hangman?(yes/no)");
           
        userinput=Keyboard.readString();
 
     do
        {
       	  String MASK_CHAR="-";
          String maskedWord="";
          String userGuess;                         
          String secretWord;
          int tries=0;                                 
         
         time++;
         
        if(userinput.equals("no"))             // if someone dont't want to paly the game
        
          {System.out.println("May be next time!");
        
            break;
           }  
           
        if(userinput.equals("yes"))           // if someone want to paly the game
          {
                                              
             {                                 
                System.out.print("Enter the word(all the lowcase):"); 
                
                                              // ask one peoson enter a secret word
             }

           secretWord=Keyboard.readString();
       
                                             // the other person begin to guess the word
      
              for(int i=0;i<secretWord.length();i++)
                  { 
                     maskedWord+=MASK_CHAR;
                   } 
          
  //-------------------------------------------------------------------------
  //Guess the word  ( oney have 10 tires)
  //-------------------------------------------------------------------------                  
              do          
                 {
                     tries++;

                    System.out.print("Make a guess:");
                    
		            userGuess=Keyboard.readString();
		            
		            for (int t=0;t<secretWord.length();t++)	 
		                                      //judge userguess ture or false
		            
		                 {	                 	 	    	  		
	  	                   if (userGuess.charAt(0)==secretWord.charAt(t))
	  	                   
		  	                {maskedWord=maskedWord.substring(0,t)+userGuess+
		    	            maskedWord.substring(t+1,secretWord.length());
		    	             } 
		                  }
		                  
		         System.out.println("Progress so far:"+maskedWord);             
	           
	                                            // if user have the right guess
                     if(maskedWord.indexOf("-")==-1)
                     
                        {
                        	System.out.println("Congratulation!You hava won!");
                           break;  
	                 
                         }
              
                 }while(tries<=9);
                 
               
           if(tries>9&&!secretWord.equals(maskedWord))
           
		        System.out.println("Bad luck,you lost -you have used all 10 of your guesses");
		     	System.out.print("Do you want to play again?(yes/no)");
		     	userinput=Keyboard.readString();     
		     	
       
		}

		 if(userinput.equals("no"))
		 
		 {
		     System.out.println("Thank you for you playing, you have played "+time+" times");
		     
			  break;                               //out of loop   Game over
		}
		
	   } while( userinput.equals("yes"));          // the loop for paly the game
	   
	   
		     
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -