<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## Java編程那些事兒50—多維數組使用示例2 鄭州游戲學院 陳躍峰 出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb) **6.6.3 存儲圖形結構** 要求:根據數組中的值,在對應位置繪制指定的字符。規定0繪制空格,1繪制星號(*)。數組的值如下所示: ??????????????????????????? { ???????????????????????????????????? {0,0,0,1,0,0,0}, ???????????????????????????????????? {0,0,1,0,1,0,0}, ???????????????????????????????????? {0,1,0,0,0,1,0}, ???????????????????????????????????? {1,0,0,0,0,0,1}, ???????????????????????????????????? {0,1,0,0,0,1,0}, ???????????????????????????????????? {0,0,1,0,1,0,0}, ???????????????????????????????????? {0,0,0,1,0,0,0} ??????????????????????????? } 該題目是一個基本的數組應用,數組中的值存儲的是控制信息,程序根據數組中的值實現規定的功能。 實現思路:循環數組中的元素,判斷數組中的值,根據值繪制對應的字符即可。 實現的代碼如下所示: ~~~ ?????????????????? int[][] map = { ???????????????????????????????????? {0,0,0,1,0,0,0}, ???????????????????????????????????? {0,0,1,0,1,0,0}, ???????????????????????????????????? {0,1,0,0,0,1,0}, ???????????????????????????????????? {1,0,0,0,0,0,1}, ???????????????????????????????????? {0,1,0,0,0,1,0}, ???????????????????????????????????? {0,0,1,0,1,0,0}, ???????????????????????????????????? {0,0,0,1,0,0,0} ?????????????????? }; ?????????????????? //輸出數組的值 ?????????????????? for(int row = 0;row < map.length;row++){ ??????????????????????????? for(int col = 0;col < map[row].length;col++){ ???????????????????????????????????? switch(map[row][col]){ ???????????????????????????????????? case 0: ?????????????????????????????????????????????? System.out.print(' '); ?????????????????????????????????????????????? break; ???????????????????????????????????? case 1: ?????????????????????????????????????????????? System.out.print('*'); ?????????????????????????????????????????????? break; ???????????????????????????????????? } ??????????????????????????? } ??????????????????????????? System.out.println(); ?????????????????? } ~~~ 類似的代碼在游戲開發中,可以用來代表游戲中的地圖數據,或者俄羅斯方塊等益智游戲中地圖塊的值。 **6.6.4 螺旋數組** 要求:存儲和輸出nXm的螺旋數組,其中n和m為大于0的整數。 以下是一些螺旋數組的示例: 1???????? ?2??3??4??????????????????? ?1??2??3??4??5 12?13?14?5???????????????????? 14 ?15 ?16 ?17?6 11 ?16?15?6???????????????????? 13 ?20 ?19 ?18 ?7 10?9??8??7???????????????????? 12 ?11 ?10 ??9 ?8 4X4螺旋數組??????????????????????? 4X5螺旋數組 對于螺旋數組來說,其中的數值很有規則,就是按照旋轉的結構數值每次加1,實現該功能需要對數組和流程控制有角深刻的認識。 實現思路:聲明一個變量來代表需要為數組元素賦的值,對于其中的數字來說,每個數字都有一個移動方向,這個方向指向下一個元素,根據該方向改變數組的下標,如果到達邊界或指向的元素已經賦值,則改變方向。 實現代碼如下: ~~~ ???????? ???????? int n = 4; ?????????????????? int m = 5; ?????????????????? int[][] data = new int[n][m]; ?????????????????? int dire;?? //當前數字的移動方向 ?????????????????? final int UP = 0;?? //上 ?????????????????? final int DOWN = 1; //下 ?????????????????? final int LEFT = 2; //左 ?????????????????? final int RIGHT = 3;//右 ?????????????????? dire = RIGHT; ?????????????????? int value = 1;??? //數組元素的值 ?????????????????? int row = 0;???? //第一維下標 ?????????????????? int col = 0;???? //第二維下標 ?????????????????? data[0][0] = 1;?//初始化第一個元素 ?????????????????? while(value < n * m){ ??????????????????????????? switch(dire){ ???????????????????????????????????? case UP: ?????????????????????????????????????????????? row--; //移動到上一行 ?????????????????????????????????????????????? if(row < 0){ //超過邊界 ??????????????????????????????????????????????????????? row++; //后退 ??????????????????????????????????????????????????????? dire = RIGHT; ?????????????????????????????????????????????? ???????? continue; //跳過該次循環 ?????????????????????????????????????????????? }else if(data[row][col] != 0){//已賦值 ??????????????????????????????????????????????????????? row++; //后退 ??????????????????????????????????????????????????????? dire = RIGHT; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? } ?????????????????????????????????????????????? break; ???????????????????????????????????? case DOWN: ?????????????????????????????????????????????? row++; //移動到下一行 ?????????????????????????????????????????????? if(row >= n){ //超過邊界 ??????????????????????????????????????????????????????? row--; //后退 ??????????????????????????????????????????????????????? dire = LEFT; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? }else if(data[row][col] != 0){//已賦值 ??????????????????????????????????????????????????????? row--; //后退 ??????????????????????????????????????????????????????? dire = LEFT; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? } ?????????????????????????????????????????????? break; ???????????????????????????????????? case LEFT: ?????????????????????????????????????????????? col--; //移動到前一列 ?????????????????????????????????????????????? if(col < 0){ //超過邊界 ??????????????????????????????????????????????????????? col++; //后退 ??????????????????????????????????????????????????????? dire = UP; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? }else if(data[row][col] != 0){//已賦值 ??????????????????????????????????????????????????????? col++; //后退 ??????????????????????????????????????????????????????? dire = UP; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? } ?????????????????????????????????????????????? break; ???????????????????????????????????? case RIGHT: ?????????????????????????????????????????????? col++; //移動到后一行 ?????????????????????????????????????????????? if(col >= m){ //超過邊界 ??????????????????????????????????????????????????????? col--; //后退 ??????????????????????????????????????????????????????? dire = DOWN; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? }else if(data[row][col] != 0){//已賦值 ??????????????????????????????????????????????????????? col--; //后退 ??????????????????????????????????????????????????????? dire = DOWN; ??????????????????????????????????????????????????????? continue; //跳過該次循環 ?????????????????????????????????????????????? } ?????????????????????????????????????????????? break; ??????????????????????????? } ??????????????????????????? value++; //數值增加1 ??????????????????????????? data[row][col] = value;//賦值??????????????????? ?????????????????? } ?????????????????? //輸出數組中的元素 ?????????????????? for(int i = 0;i < data.length;i++){ ??????????????????????????? for(int j = 0;j < data[i].length;j++){ ???????????????????????????????????? if(data[i][j] < 10){//右對齊 ?????????????????????????????????????????????? System.out.print(' '); ???????????????????????????????????? } ???????????????????????????????????? System.out.print(data[i][j]); ???????????????????????????????????? System.out.print(' '); ??????????????????????????? } ??????????????????????????? System.out.println(); ?????????????????? } ~~~ 在該代碼中dire代表當前元素的移動方向,每個根據該變量的值實現移動,如果移動時超出邊界或移動到的位置已賦值,則改變方向,并跳過本次循環,如果移動成功,則數值增加1,對數組元素進行賦值。 對于多維數組來說,更多的是設計數組的結構,并根據邏輯的需要變換數組的下標,實現對于多維數組元素的操作。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看