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

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

如何使用表中的 if 函數在循環中排列重復字符串數組

如何使用表中的 if 函數在循環中排列重復字符串數組

翻翻過去那場雪 2023-09-20 17:07:48
我想將隨機輸入字符串數組排列到一個表中,看起來使用循環和 if 函數或更簡單的東西。有最小、小、中、大、最大之分。并且字符串各重復 x5該數組是:    String cow[][] = new String[5][5];    cow[0][0] = "big";    cow[0][1] = "smallest";    cow[0][2] = "small";    cow[0][3] = "medium";    cow[0][4] = "biggest";    cow[1][0] = "smallest";    cow[1][1] = "biggest";    cow[1][2] = "medium";    cow[1][3] = "small";    cow[1][4] = "big";    cow[2][0] = "medium";    cow[2][1] = "biggest";    cow[2][2] = "big";    cow[2][3] = "smallest";    cow[2][4] = "small";    cow[3][0] = "small";    cow[3][1] = "big";    cow[3][2] = "smallest";    cow[3][3] = "medium";    cow[3][4] = "biggest";    cow[4][0] = "biggest";    cow[4][1] = "medium";    cow[4][2] = "big";    cow[4][3] = "small";    cow[4][4] = "smallest";我的排列數組的代碼:for (int j = 0; j < cow.length; j++) {    for (int i = 0; i < cow[j].length; i++) {        if (cow[i][j] == "smallest") {            System.out.print("| " + cow[i][j] + " |");        } else if (cow[i][j] == "small") {            System.out.print("| " + cow[i][j] + " |");        } else if (cow[i][j] == "medium") {            System.out.print("| " + cow[i][j] + " |");        } else if (cow[i][j] == "big") {            System.out.print("| " + cow[i][j] + " |");        } else if (cow[i][j] == "biggest") {            System.out.print("| " + cow[i][j] + " |");        }    }    System.out.println();}當我運行代碼時,我只得到最小的 |最小|最小|最小|最小|我認為這是因為循環沒有為其他索引重新循環(?),所以表不是 5x5我所期望的:smallest | small | medium | big | biggestsmallest | small | medium | big | biggestsmallest | small | medium | big | biggestsmallest | small | medium | big | biggestsmallest | small | medium | big | biggest請教我,因為我對java很陌生。謝謝
查看完整描述

2 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

在這里,我們使用 ArrayList 來存儲每一行,并使用 Collections 類對其進行排序,然后我們為每一行使用 StringJoiner 在值之間添加管道,您可以在代碼注釋中看到詳細信息。并記住導入所有必要的類


String cow[][] = new String[5][5];


    cow[0][0] = "big";

    cow[0][1] = "smallest";

    cow[0][2] = "small";

    cow[0][3] = "medium";

    cow[0][4] = "biggest";


    cow[1][0] = "smallest";

    cow[1][1] = "biggest";

    cow[1][2] = "medium";

    cow[1][3] = "small";

    cow[1][4] = "big";


    cow[2][0] = "medium";

    cow[2][1] = "biggest";

    cow[2][2] = "big";

    cow[2][3] = "smallest";

    cow[2][4] = "small";


    cow[3][0] = "small";

    cow[3][1] = "big";

    cow[3][2] = "smallest";

    cow[3][3] = "medium";

    cow[3][4] = "biggest";


    cow[4][0] = "biggest";

    cow[4][1] = "medium";

    cow[4][2] = "big";

    cow[4][3] = "small";

    cow[4][4] = "smallest";


    for (int i = 0; i < cow.length; i++) {

        List<String> list = new ArrayList<>(); //list for saving each row

        for (int j = 0; j < cow[i].length; j++) {


            list.add(cow[i][j]);


        }

        Collections.sort(list); // sort array

        Collections.reverse(list); // reverse array just for better order

        String big = list.get(4);  // since the biggest comes befor big

        list.set(4, list.get(3)); // we need to change them

        list.set(3, big);


        for(int x=0;x<list.size();x++){

if(x<list.size()-1){  System.out.print(list.get(x)+" | "); }

else { System.out.println(list.get(x)); } }


    } // end of outer loop


查看完整回答
反對 回復 2023-09-20
?
喵喔喔

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

您需要使用 equals 方法比較兩個字符串。

if ((cow[i][j]).equals("small")) {
........ }


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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