亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何運行多個if語句

如何運行多個if語句

呼如林 2021-10-20 15:21:36
當我運行這段代碼時,它只會執行一些 if 語句,直到找到正確的語句為止。如何讓它貫穿所有 if 語句。for (int c = 0; c <= 9; c++) {    for (int r = 0; r <= 9; r++) {        try {            if (Sprite.mine_1[c - 1][r - 1] < 20) Sprite.count++; //top left            System.out.println("if 1");            if (Sprite.mine_1[c - 1][r] < 20) Sprite.count++; //left            System.out.println("if 2");            if (Sprite.mine_1[c - 1][r + 1] < 20) Sprite.count++;  //buttom left            System.out.println("if 2");            if (Sprite.mine_1[c][r + 1] < 20) Sprite.count++;  //buttom                System.out.println("if 4");            if (Sprite.mine_1[c + 1][r + 1] < 20) Sprite.count++;  //buttom right              System.out.println("if 5");            if (Sprite.mine_1[c + 1][r] < 20) Sprite.count++;  // right                System.out.println("if 6");            if (Sprite.mine_1[c + 1][r - 1] < 20) Sprite.count++;  // right to            System.out.println("if 7");            if (Sprite.mine_1[c][r - 1] < 20) Sprite.count++;  //top            System.out.println("if 8");        } catch (ArrayIndexOutOfBoundsException exception) {        }    }}
查看完整描述

2 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

我只是最終做了很長的路。


    for(int c=0; c<=9; c++)//if number 1

    {

        for(int r=0; r<=9; r++)

        {       

            try 

        {


        if(Sprite.mine_1[c-1][r-1]<20) Sprite.mine_2 [c][r] +=1; //top left




        }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }       


    for(int c=0; c<=9; c++)// number2

    {

        for(int r=0; r<=9; r++)

        {       

        try 

        {


        if(Sprite.mine_1[c-1][r  ]<20) Sprite.mine_2 [c][r] +=1; //left



    }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }


            for(int c=0; c<=9; c++)// number3

    {

        for(int r=0; r<=9; r++)

        {       

        try 

        {


        if(Sprite.mine_1[c-1][r+1]<20) Sprite.mine_2 [c][r] +=1;    //buttom left



    }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }



            for(int c=0; c<=9; c++)// number4

    {

        for(int r=0; r<=9; r++)

        {       

        try 

        {


        if(Sprite.mine_1[c  ][r+1]<20) Sprite.mine_2 [c][r] +=1;    //buttom    



    }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }



            for(int c=0; c<=9; c++)// number5

    {

        for(int r=0; r<=9; r++)

        {       

        try 

        {


        if(Sprite.mine_1[c+1][r+1]<20) Sprite.mine_2 [c][r] +=1;    //buttom right  



    }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }




            for(int c=0; c<=9; c++)// number6

    {

        for(int r=0; r<=9; r++)

        {       

        try 

        {


        if(Sprite.mine_1[c+1][r  ]<20) Sprite.mine_2 [c][r] +=1;    // right    



    }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }




            for(int c=0; c<=9; c++)// number7

    {

        for(int r=0; r<=9; r++)

        {       

        try 

        {


        if(Sprite.mine_1[c+1][r-1]<20) Sprite.mine_2 [c][r] +=1;    // top right



    }

        catch(ArrayIndexOutOfBoundsException exception) 

        {               

        }       


        }

    }



查看完整回答
反對 回復 2021-10-20
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

由于多級循環,此解決方案將非常耗時,但這仍然可以檢查特定 c 和 r 值的所有情況......


for (int c = 0; c <= 9; c++)

{

    for (int r = 0; c <= 9; r++)

    {

        for (int i = -1; c <= 1; i++)

        {

            for (int j = -1; c <= 1; j++)

            {

               try

               {

                   if(Sprite.mine_1[c+i][r+j] < 20 && ( i != 0 || j != 0 ) )

                  {

                     Sprite.count++; 

                  }  

               catch (Exception e)


                {

                  // print what you want

                }

             }


         }


    }


}

對于更好的情況,請嘗試這樣的操作以避免 IndexOutOfBoundException 以 1 到 10 但實際數組為 0 - 11 開始 c 和 r


for (int c = 1; c <= 10; c++)

{

    for (int r = 1; c <= 10; r++)

    {

        for (int i = -1; c <= 1; i++)

        {

            for (int j = -1; c <= 1; j++)

            {

               try

               {

                   if(Sprite.mine_1[c+i][r+j] < 20 && ( i != 0 || j != 0 ) )

                  {

                     Sprite.count++; 

                  }  

               catch (Exception e)


                {

                  // print what you want

                }

             }


         }


    }


}


查看完整回答
反對 回復 2021-10-20
  • 2 回答
  • 0 關注
  • 245 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號