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

📄 hangman06.java

📁 this is a game named hangman.
💻 JAVA
字号:
//********************************************************************
// Hangman06.java         Author:ZhaoHaiyan
// A program for two person game of hangman.The game allows the first
// to enter the word to be guessed and the second person to guess 
// letter in the word
//********************************************************************

import java.util.Scanner;
public class Hangman06
{
	public static void main(String[]args)
	{
	   String secretWord,ans;    //secretWord is the word to be guessed
	   String MASK_CHAR="-";
	   String maskedWord=" ";
	   String s1="yes",s2="no";
	   char userGuess;          //char the second user guesses
	   int counter=0;           //times the user played
	   final int max_chance=10; //the maximum chances user has
	   
	   Scanner scan=new Scanner(System.in);
	   
	   System.out.print("Do you want to play a game of hangman?");
	   ans=scan.next();
	   
	   // read the answer and  decide whether to play the game
	   
	   if(ans.equals(s2))
	    {
	   	   System.out.println("Maybe next time!");
	    }
	   else
	    {
	    	label:while(ans.equals(s1)) //declare a label
	    	{
	    	  int num=0;           // in the second time, the num should be restart
	          maskedWord="-";     
	          
	          System.out.print("Enter secret word(all in lowercase):");
	          secretWord=scan.next();
	          
	          for(int i=0;i<secretWord.length();i++)
	             maskedWord+=MASK_CHAR;
	      /* fill an empty string so that it contains the same number of characters
	         as the string secretWord,but all blanks   
	      */   
	       do
	          {
	          	System.out.print("Make a guess:");
	          	userGuess=scan.next().charAt(0);
	          	
	          	for(int i=0;i<secretWord.length();i++)
	          	{
	          		if(secretWord.charAt(i)==userGuess)
	          		   maskedWord=maskedWord.substring(0, i)
	          		              +userGuess
	          		              +maskedWord.substring(i+1,secretWord.length()); 
	          	}
	          	System.out.println("Progress so far:"+maskedWord);
	          	//replace character i in the string maskedWord with user's guess
	          	
	          	if(maskedWord.indexOf('-')==-1)
	          	//there are no blanks left in the masked string
	          	{
	          		System.out.println("Congratulations,you won!");
	          		counter++;
	          		
	          		System.out.print("Do you want to play again?");
	          		ans=scan.next();
	          		
	          		if(ans.equals(s1))
	          		    continue  label;//restart the label
	          		
	          		else 
	          		{
	          			System.out.println("Thanks for playing!"+"you played "
	          			                   +counter+" times!"); 
	          			 System.exit(0);  //exit the game                 	                   
	          		}
	          	}
	          	num++;
	          }while(num<max_chance);
	          //when the user use less than or equal to 10 chances
	          counter++;
	          
	          System.out.println("bad luck!you lost- you'v used all 10 of your guesses!");
	          System.out.print("Do you want to play again?");
	    	  ans=scan.next();	
	    	  
	    	  if (ans.equals(s1))
	    	     continue label;
	    	   
	    	   else
	    	   {
	    	   	  System.out.println("Thanks for playing!"+"You played "
	    	   	                      + counter+" times!");	                     
	    	   }  
	    	}
	   }
	}	
}

⌨️ 快捷键说明

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