Java 为什么我的词典程序不能打印所有三个字符串?

Java 为什么我的词典程序不能打印所有三个字符串?,java,Java,正如你所看到的,我有三个名字,分别是:s1、s2和s3。它应该把字母顺序放在新的一行上,但是艾希礼没有打印。在这里输入代码 您正在使逻辑复杂化,因为您试图一次比较太多的内容,这将使理解和阅读逻辑变得更加困难,而且您的代码缩进也不好 您可以尝试下面带有内联注释的代码,这些代码将进行逐步比较并获得结果 if(s1.compareTo(s2) < 0) { //is s1 higher than s2 ? if(s1.compareTo(

正如你所看到的,我有三个名字,分别是:s1、s2和s3。它应该把字母顺序放在新的一行上,但是艾希礼没有打印。在这里输入代码


您正在使逻辑复杂化,因为您试图一次比较太多的内容,这将使理解和阅读逻辑变得更加困难,而且您的代码缩进也不好

您可以尝试下面带有内联注释的代码,这些代码将进行逐步比较并获得结果

             if(s1.compareTo(s2) < 0) { //is s1 higher than s2 ?
                if(s1.compareTo(s3) < 0) { //is s1 higher than s3 as well ?
                    topString = s1;//then topper is s1
                    if(s2.compareTo(s3) < 0) {//is s2 higher than s3 ?
                        midString = s2;//then mid is s2 
                        botString = s3;//bot is s3
                    }
                } else {
                    topString = s3;//s3 is topper compared to s1 and s2
                    midString = s1;//mid is s1
                    botString = s2;//bot is s2
                }
             } else {//means s2 is topper
                if(s2.compareTo(s3) < 0) {//is s2 topper than s3 as well ?
                    topString = s2;//yes, so s2 is topper
                    if(s1.compareTo(s3) < 0) {//is s1 or s3 who is first ?
                        midString = s1;//s1 is first, so at middle
                        botString = s3;//s3 is bot
                    }
                } else {//s2 is not topper than s3, but topper than s2
                    topString = s3;//top is s3
                    midString = s2;//mid is s2
                    botString = s1;//bot is s1
                }
            }

看起来你在重新发明轮子。使用Arrays.sort或Collections。sort@4castle我应该这样做。我知道做数组要容易得多,但我必须在课堂上这样做,因为我们还没有学会数组。欢迎来到堆栈溢出!看起来您需要学习使用调试器。请随便吃点。如果您以后仍然有问题,请随时回来提供更多详细信息。
             if(s1.compareTo(s2) < 0) { //is s1 higher than s2 ?
                if(s1.compareTo(s3) < 0) { //is s1 higher than s3 as well ?
                    topString = s1;//then topper is s1
                    if(s2.compareTo(s3) < 0) {//is s2 higher than s3 ?
                        midString = s2;//then mid is s2 
                        botString = s3;//bot is s3
                    }
                } else {
                    topString = s3;//s3 is topper compared to s1 and s2
                    midString = s1;//mid is s1
                    botString = s2;//bot is s2
                }
             } else {//means s2 is topper
                if(s2.compareTo(s3) < 0) {//is s2 topper than s3 as well ?
                    topString = s2;//yes, so s2 is topper
                    if(s1.compareTo(s3) < 0) {//is s1 or s3 who is first ?
                        midString = s1;//s1 is first, so at middle
                        botString = s3;//s3 is bot
                    }
                } else {//s2 is not topper than s3, but topper than s2
                    topString = s3;//top is s3
                    midString = s2;//mid is s2
                    botString = s1;//bot is s1
                }
            }