Hangman

文章目录


前言

hangman游戏界面

二、使用步骤

package game;

import java.util.Scanner;
import java.util.Random;
public class Hangman {
//	String word = "australia";
	String word[] = {"japan","qatar","syria","mongolia","bahrain","india"};
	public void showMenu() {
		System.out.println("-----Menu-----");
		System.out.println("1.Play");
		System.out.println("2.Instructions");
		System.out.println("3.Exit");
		System.out.print("\nChoose the option:");
		int option;
		Scanner sc = new Scanner(System.in);
		System.out.println("\n输入数字");
		option = sc.nextInt();
		switch (option) {
		case 1:
			playGame();
			break;
		case 2:
			instructGame();
			break;
		case 3:
			exitGame();
			break;
		default:
			System.out.println("Incorrect menu option");
			showMenu();
			break;

		}

	}
public void playGame() {
		System.out.println("PlayGame method is invoked");
		/*
		 * int flag = 0;
		String input ,guess;
		Scanner sc = new Scanner(System.in);
		do {
		System.out.println("\nEnter your guess");
		input = sc.nextLine();
		for(int i  = 0;i<word.length();i++) {
			if(word.charAt(i) == input.charAt(0)) {
				flag = 1;
				}
		}
		if(flag == 1) {
			System.out.println("this letter is present in the word");
		}else {
			System.out.println("this letter is not present in the word " );
		}
		System.out.println("Do want to guess again(y/n)");
		guess = sc.nextLine();
		flag = 0;
	}while(guess.equals("y")||guess.equals("Y"));
	*/
		int len,count = 0,rnd,flag = 0;
		String choice,temp;
		Random rd = new Random();
		Scanner input = new Scanner(System.in);
		rnd = rd.nextInt(6);
		len = word[rnd].length();
		char[] newString = new char[len];
		StringBuffer wrgString = new StringBuffer();
		for(int j = 0;j <len;j++) {
			System.out.println("_ ");
		}
		System.out.println();
		do {
			flag = 0;
			System.out.println("\n\nEnter your guess");
			String ch = input.nextLine().toLowerCase();
			count++;
			for(int i = 0;i<len;i++) {
				if(word[rnd].charAt(i) == ch.charAt(0)) {
					newString[i] = word[rnd].charAt(i);
					flag = 1;
				}
			}
			if(flag == 0) {
				flag = 1;
				wrgString.append(ch+",");
				System.out.println("\nMisses:" + wrgString);
			}
			System.out.println(newString);
			temp = new String(newString);
		    if(word[rnd].equals(temp)) {
		    	System.out.println("----Congrats :)You won----");
		    	System.out.println("Do you want to play again(y/n)");
		    	choice = input.nextLine();
		    	if(choice.equalsIgnoreCase("y")) {
		    		playGame();
		    	}else {
		    		showMenu();
		    	}
		    }
		}while(flag != 0);
		
}
public void instructGame() {
	System.out.println("instructGame method is invoked");
}
public void exitGame() {
	System.out.println("exitGame method is invoked");
	System.exit(0);
}
	public static void main(String[] args) {
		Hangman hg = new Hangman();
		hg.showMenu();
	}
	


}

总结

学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哩吇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值