5-2-4 二维数组-遍历-初始化-井字过三关

之前介绍的数组都是1维的,一个下标一个元素。

实际上可以是多维的


引入二维数组


        int [][] a = new int [3] [5];

通常理解为a是3行5列的矩阵。

三维就3对[]


二维数组的遍历:

        for ( int i = 0 ; i < 3; i++ )
        {
        	for ( int j = 0; j < 5; j++ )
        	{
        		a[i][j] = i * j;
        	}
        }


留意:

1.a[i][j]是一个int

2.表示第i行第j列上的单元


3.a[i,j]不存在


二维数组的初始化

<p class="p1">        <span class="s1">int</span> [] [] <span class="s2">a</span> = {</p><p class="p1">        <span>	</span>{1,2,3,4},</p><p class="p1">        <span>	</span>{1,2,3},</p><p class="p1">        };</p>

表示两行四列,最后一个自动补零



留意:

1.编译器会自己数数

2.每行一个{},逗号分开

3.最后的逗号可以存在,有这个传统

4.如果省略,表示补零


例子——tic-tac-toe游戏——井字过三关:


程序要输出是否有获胜方,是X还是O



自己整理老师的程序,未测试运行:

import java.util.Scanner;

public class Main {    
    
    public static void main(String[] args) {                  
        Scanner in = new Scanner(System.in);
    	
//      表达大小的常量
        final int SIZE = 3;
//      矩阵        
        int [][] board = new int [SIZE] [SIZE];
//      有无得到结果
        boolean gotResult = false;
//      做4次判断——数数是否一行3个X或者O
        int numOfX = 0;
        int numOfO = 0;
        
        int i ;
        int j ;
        
//      读入矩阵——board.length,表示有几行        
        for (  i = 0; i < board.length  ; i++)
        {
//        	某一行有几个——即表示有几列
        	for ( j = 0; j < board[i].length ; j++)
        	{
        		board [i][j] = in.nextInt(); 
        	}
        }
        
//      检查行
        if ( !gotResult )
        {
        	numOfX = 0;
        	numOfO = 0;
        	for ( int i = 0; i < board.length  ; i++)
        	{
        		for ( j = 0; j < board[i].length ; j++)
            	{
        			if ( board [i][j] = 1 )
        			{
        				numOfX ++;
        			}
        			else
        			{
        				numOfO ++;
        			}
            	}
        		if ( numOfX == SIZE || numOfO == SIZE )
        		{
        			gotResult = true;
        		}
        	}	
        }
        
//      检查列
        if ( !gotResult )
        {
        	numOfX = 0;
        	numOfO = 0;
        	for ( int j = 0; j < board.length  ; j++)
        	{
        		if ( board [j][i] = 1 )
        		{
        			numOfX ++;
        		}
        		else
        		{
        			numOfO ++;
        		}
        	}
        	if ( numOfX == SIZE || numOfO == SIZE )
        	{
        		gotResult = true;
        	}
        }
        	
//      检测对角线
        if ( !gotResult )
        {
        	numOfX = 0;
        	numOfO = 0;
        	for ( j = 0; j < board.length  ; j++)
        	{
        		if ( board [i] [i] == 1)
        		{
        			numOfX ++;
        		}
        		else
        		{
        			numOfO ++;
        		}
        	}
        	
        	if ( numOfX == SIZE || numOfO == SIZE )
        	{
        		gotResult = true;
        	}
        }
        
//      检测反对角线
        if ( !gotResult )
        {
        	numOfX = 0;
        	numOfO = 0;
        	for ( j = 0; j < board.length  ; j++)
        	{
        		if ( board [i] [SIZE - i - 1 ] == 1)
        		{
        			numOfX ++;
        		}
        		else
        		{
        			numOfO ++;
        		}
        	}
        	
        	if ( numOfX == SIZE || numOfO == SIZE )
        	{
        		gotResult = true;
        	}
        }
        
        if ( gotResult )
        {
        	if ( numOfX == SIZE )
        	{
        		System.out.println("x赢了");
        	}
        	else 
        	{
        		System.out.println("o赢了");
        	}
        }
        
               
    }		

}  


对角线和反对角线只要一层循环





































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值