如何使用表中的 if 函数在循环中排列重复字符串数组

我想将随机输入字符串数组排列到一个表中,看起来使用循环和 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 | biggest

smallest | small | medium | big | biggest

smallest | small | medium | big | biggest

smallest | small | medium | big | biggest

smallest | small | medium | big | biggest

请教我,因为我对java很陌生。谢谢


翻翻过去那场雪
浏览 63回答 2
2回答

繁星coding

在这里,我们使用 ArrayList 来存储每一行,并使用 Collections 类对其进行排序,然后我们为每一行使用 StringJoiner 在值之间添加管道,您可以在代码注释中看到详细信息。并记住导入所有必要的类String cow[][] = new String[5][5];&nbsp; &nbsp; cow[0][0] = "big";&nbsp; &nbsp; cow[0][1] = "smallest";&nbsp; &nbsp; cow[0][2] = "small";&nbsp; &nbsp; cow[0][3] = "medium";&nbsp; &nbsp; cow[0][4] = "biggest";&nbsp; &nbsp; cow[1][0] = "smallest";&nbsp; &nbsp; cow[1][1] = "biggest";&nbsp; &nbsp; cow[1][2] = "medium";&nbsp; &nbsp; cow[1][3] = "small";&nbsp; &nbsp; cow[1][4] = "big";&nbsp; &nbsp; cow[2][0] = "medium";&nbsp; &nbsp; cow[2][1] = "biggest";&nbsp; &nbsp; cow[2][2] = "big";&nbsp; &nbsp; cow[2][3] = "smallest";&nbsp; &nbsp; cow[2][4] = "small";&nbsp; &nbsp; cow[3][0] = "small";&nbsp; &nbsp; cow[3][1] = "big";&nbsp; &nbsp; cow[3][2] = "smallest";&nbsp; &nbsp; cow[3][3] = "medium";&nbsp; &nbsp; cow[3][4] = "biggest";&nbsp; &nbsp; cow[4][0] = "biggest";&nbsp; &nbsp; cow[4][1] = "medium";&nbsp; &nbsp; cow[4][2] = "big";&nbsp; &nbsp; cow[4][3] = "small";&nbsp; &nbsp; cow[4][4] = "smallest";&nbsp; &nbsp; for (int i = 0; i < cow.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; List<String> list = new ArrayList<>(); //list for saving each row&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < cow[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(cow[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(list); // sort array&nbsp; &nbsp; &nbsp; &nbsp; Collections.reverse(list); // reverse array just for better order&nbsp; &nbsp; &nbsp; &nbsp; String big = list.get(4);&nbsp; // since the biggest comes befor big&nbsp; &nbsp; &nbsp; &nbsp; list.set(4, list.get(3)); // we need to change them&nbsp; &nbsp; &nbsp; &nbsp; list.set(3, big);&nbsp; &nbsp; &nbsp; &nbsp; for(int x=0;x<list.size();x++){if(x<list.size()-1){&nbsp; System.out.print(list.get(x)+" | "); }else { System.out.println(list.get(x)); } }&nbsp; &nbsp; } // end of outer loop

喵喔喔

您需要使用 equals 方法比较两个字符串。if&nbsp;((cow[i][j]).equals("small"))&nbsp;{ ........&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java