温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-04 17:12:28 来源:ITPUB博客 阅读:191 作者:huyang_ya 栏目:编程语言

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI