java 实现一个简单的猜字符小游戏

用java的一些基础知识来实现一个简单的猜字母的小游戏,就在控制台玩玩,目得也是为了熟悉熟悉java的基础知识。

首先系统随机产生5个字母,然后我们认为输入5个字母,输入的字母和系统随机产生的字母做比较,只有当5个字母顺序完全正确才算猜对。总分为500分,猜错一次扣10分,输入“exit”退出游戏。

代码如下:

package com.test.basics;

import java.util.Scanner;

public class GuessGame {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		char[] chs = generator();
		StringBuffer rand = new StringBuffer();
		for(char ch:chs) {
			rand.append(ch);
		}
		System.out.println("随机产生的字符为:"+rand.toString());//作弊,否则不好猜啊
		int count = 0;
		while(true) {
			System.out.println("请输入五个字母:");
			String str = scan.nextLine().toUpperCase();//获取输入的字符串,并将其转化为全大写
			if(str.equals("EXIT")) {
				System.out.println("欢迎下次再来!");
				break;
			}
			char[] input = str.toCharArray();
			int[] result = check(chs, input);
			if(result[0]==input.length) {
				int score = 100*input.length-10*count;
				System.out.println("恭喜你,猜对了,得分为"+score);
				break;
			}else {
				count++;
				System.out.println("本次位置对的字母个数为:"+result[0]+";字母正确的个数为:"+result[1]);
			}
			
		}
	}
	
	//产生随机字符的方法
	public static char[] generator() {
		char[] chs = new char[5];
		char[] letters = new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
		           				'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
		        				'W', 'X', 'Y', 'Z' };
		boolean[] flags = new boolean[letters.length];
		for(int i=0;i<chs.length;i++) {
			int index;
			do {
				index = (int)(Math.random()*letters.length);
			}while(flags[index]==true);
			chs[i] = letters[index];
			flags[index] = true;
		}
		return chs;
	}
	
	//随机字符和输入的字符对比的结果
	public static int[] check(char[] chs,char[] input) {
		int[] result = new int[2];//result[0]表示位置猜对了,result[1]表示字符猜对了
		for(int i=0;i<chs.length;i++) {
			for(int j=0;j<input.length;j++) {
				if(chs[i]==input[j]) {
					result[1]++;
					if(i==j) {
						result[0]++;
					}
					break;
				}
			}
		}
		return result;
	}
	
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以参考以下代码:import java.util.Scanner;public class BoxGame {public static void main(String[] args) {// 定义游戏地图int[][] map = { { 0, 0, 0, 0, 0 }, { 0, 1, 2, 1, 0 }, { 0, 1, 0, 0, 0 }, { 0, 0, 1, 3, 0 }, { 0, 0, 0, 0, 0 } };// 游戏开始System.out.println("游戏开始!");// 定义人物位置int x = 1;int y = 1;// 开始玩游戏while (true) {// 打印游戏地图for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { if (i == x && j == y) { System.out.print("@ "); } else { System.out.print(map[i][j] + " "); } } System.out.println(); }// 移动Scanner scanner = new Scanner(System.in);System.out.println("请输入您要移动的方向:(w上 s下 a左 d右)");String direction = scanner.nextLine();// 判断是否移动成功boolean flag = false;switch (direction) {case "w":if (map[x - 1][y] == 0) { x = x - 1; flag = true; }break;case "s":if (map[x + 1][y] == 0) { x = x + 1; flag = true; }break;case "a":if (map[x][y - 1] == 0) { y = y - 1; flag = true; }break;case "d":if (map[x][y + 1] == 0) { y = y + 1; flag = true; }break;default:System.out.println("输入有误,请重新输入!");break;if (!flag) { continue; }// 判断是否推箱子成功if (map[x][y] == 2) { if (direction.equals("w") && map[x - 1][y] == 1) { map[x - 1][y] = 2; flag = true; } else if (direction.equals("s") && map[x + 1][y] == 1) { map[x + 1][y] = 2; flag = true; } else if (direction.equals("a") && map[x][y - 1] == 1) { map[x][y - 1] = 2; flag = true; } else if (direction.equals("d") && map[x][y + 1] == 1) { map[x][y + 1] = 2; flag = true; } }// 判断是否胜利if (map[3][3] == 2) { System.out.println("恭喜你,你赢得了游戏!"); break; }}}}}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值