第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用表中的 if 函數(shù)在循環(huán)中排列重復(fù)字符串?dāng)?shù)組

如何使用表中的 if 函數(shù)在循環(huán)中排列重復(fù)字符串?dāng)?shù)組

翻翻過去那場雪 2023-09-20 17:07:48
我想將隨機(jī)輸入字符串?dāng)?shù)組排列到一個(gè)表中,看起來使用循環(huán)和 if 函數(shù)或更簡單的東西。有最小、小、中、大、最大之分。并且字符串各重復(fù) x5該數(shù)組是:    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";我的排列數(shù)組的代碼: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();}當(dāng)我運(yùn)行代碼時(shí),我只得到最小的 |最小|最小|最小|最小|我認(rèn)為這是因?yàn)檠h(huán)沒有為其他索引重新循環(huán)(?),所以表不是 5x5我所期望的:smallest | small | medium | big | biggestsmallest | small | medium | big | biggestsmallest | small | medium | big | biggestsmallest | small | medium | big | biggestsmallest | small | medium | big | biggest請教我,因?yàn)槲覍ava很陌生。謝謝
查看完整描述

2 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

在這里,我們使用 ArrayList 來存儲(chǔ)每一行,并使用 Collections 類對其進(jìn)行排序,然后我們?yōu)槊恳恍惺褂?StringJoiner 在值之間添加管道,您可以在代碼注釋中看到詳細(xì)信息。并記住導(dǎo)入所有必要的類


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


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

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

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

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


查看完整回答
反對 回復(fù) 2023-09-20
  • 2 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號