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

转载于 :  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