ITPub博客

首页 > 应用开发 > Java > Java 数组 之 二维数组 扫雷实例

Java 数组 之 二维数组 扫雷实例

Java 作者:huyang_ya 时间:2018-08-20 07:39:13 0 删除 编辑

转载于 :  http://www.verejava.com/?id=17159392878528  

import java.util.Scanner;public class Test1 {	/**
	 * 模拟扫雷游戏, 在一个二维数组中, 1:无雷 2:有雷, 键盘输入 行号和列号, 判断是否扫到了雷. 
	 */
	public static void main(String[] args) {		int[][] thundes = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 2, 1, 1 }, { 1, 1, 1, 1 } };
		Scanner in = new Scanner(System.in);		//键盘输入 行号和列号
		System.out.println("请输入行号:");		int row = in.nextInt();
		System.out.println("请输入列号:");		int col = in.nextInt();		int value = thundes[row][col];//从数组中获得的值
		//判断是否扫到了雷
		for (int i = 0; i < thundes.length; i++) {			for (int j = 0; j < thundes[i].length; j++) {				//判断 value 是否在 数组中存在 并且 等于 2
				if (value == thundes[i][j] && value == 2) {
					System.out.print("雷");
				} else {
					System.out.print("* ");
				}
			}
			System.out.println("");
		}
	}
}

转载于 :  http://www.verejava.com/?id=17159392878528  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10054744/viewspace-2212090/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
10+ 年软件和互联网研发架构经验,喜爱编程 , 读书

注册时间:2013-05-02

  • 博文量
    55
  • 访问量
    35712