【java】简单实现4A0B(简单猜数字)小游戏

4A0B猜数字(简单猜数字)

import java.util.Scanner;
/*
 * num类
 * 定义了一个类 
 * a,b,c,d分别表示游戏中要猜的四个数字
 * count为游戏循环计数器
 */
class num {
	int a, b, c, d;
	int count = 1;
}

public class game_4A0B {

	public static void main(String[] args) {
		num N = new num();
		all(N);
	}
/*
 * Input方法
 * 从键盘键入四个数字
 * 分别赋值给num类的N中的a,b,c,d四个值
 * 完成数组储存
 */
	private static int[] Input(num N) {
		Scanner reader = new Scanner(System.in);
		System.out.println("猜数字游戏 请输入四个数字:");
		int a = reader.nextInt();
		int b = reader.nextInt();
		int c = reader.nextInt();
		int d = reader.nextInt();
		N.a = a;
		N.b = b;
		N.c = c;
		N.d = d;
		int[] input = { a, b, c, d };
		return input;
	}
	/*
	 * private static int[] Input() { int[] datas = {1,5,6,7}; return datas; }
	 */
/*
 * game_result()方法
 * 将要答案数字用sources数组来表示
 * 用count数组来表示OAOB中的O的多少
 * count[0]即为A的个数 count[1]即为B的个数
 * 然后通过for循环来判断存入N中的
 * a,b,c,d与答案数字是否相等 
 * 如果位置和数字都相等则A的数量加一
 * 如果位置不等 但是有相同数字则B的数量加一
 * 最终返回count数组
 */
	private static int[] game_result(num N) {
		int[] datas = { N.a, N.b, N.c, N.d };
		int[] sources = { 4, 5, 6, 7 };
		int[] count = { 0, 0 };
		for (int i = 0; i < datas.length; i++) {
			for (int j = 0; j < datas.length; j++) {
				if (sources[i] == datas[j] & j == i) {
					count[0]++;
				} else if (sources[i] == datas[j] & j != i) {
					count[1]++;
				}
			}
		}
		return count;
	}
/*
 * judge()方法
 * 判断game_result()方法返回数组result[]的数值
 * 如果A的大小为4则返回true
 * 其余情况返回false
 */
	private static boolean judge(num N) {
		int[] result = game_result(N);
		int count = 1;
		if (result[0] == 4) {
			return true;
		} else {

			return false;

		}
	}
/*
 * 输出OAOB
 */
	private static void printshow(num N) {
		int[] result = game_result(N);
		System.out.printf("%dA%dB", result[0], result[1]);
		System.out.println();
	}
/*
 * show()方法
 * 根据judge()方法的返回值
 * 来判断程序是否要进行下去
 * 猜对了则祝贺玩家并退出游戏
 * 猜错了询问玩家是否继续
 */

	private static void show(num N) {
		boolean n = judge(N);
		if (n) {
			System.out.println("congratulations!");
			System.exit(0);
		} else {
			System.out.println("mistake!try it again ,man! Y/N");
		}
	}
	/*
	 * choice()方法
	 * 从键盘录入Y或者N
	 * Y的情况则再次进入程序循环
	 * 当尝试次数超过8次 会直接显示失败
	 * N的情况则直接默认游戏失败 结束程序
	 */

	private static void choice(num N) {
		Scanner reader = new Scanner(System.in);
		String n = reader.nextLine();
		if ("Y".equals(n)) {
			System.out.printf("the %d times", ++N.count);
			System.out.println();
			if (N.count > 8) {
				System.out.println("sorry,time out,game over!");
			} else {
				all(N);
			}
		} else if ("N".equals(n)) {
			System.out.println("game over");
		} else {
			System.out.println("hi man choice Y or N!");
			choice(N);
		}
	}
/*
 * all()方法
 * 调用之前的所有方法实现程序的运行
 */
	private static void all(num N) {
		Input(N);
		printshow(N);
		show(N);
		choice(N);

	}
	/*
	 * private static void show(){ judge(); }
	 */

	/*
	 * boolean n = judge(); if (n) System.out.println("恭喜你答对了"); else if (!n)
	 * System.out.println("再试一次");
	 */
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值