简体   繁体   English

Java,按字母顺序对字符串进行排序,不包含数组

[英]Java, sort strings alphabetically without arrays

So I have another assignment to do and the task is to assort 3 strings alphabetically using the compareTo method. 因此,我还有另一项工作要做,任务是使用compareTo方法按字母顺序对3个字符串进行分类。 Basically the program receives 3 strings (a, b, and c) from a tester class and its supposed to return back the "getMin", "getMiddle", and "getMax". 基本上,程序从测试器类接收3个字符串(a,b和c),并且应该返回“ getMin”,“ getMiddle”和“ getMax”。

I figured out the getmin and max, seemed easy but im having problems with the getMiddle. 我弄清楚了getmin和max,似乎很容易,但是我在getMiddle中遇到了问题。 this is what i have for min and max: 这就是我的最小和最大:

        String min = "";
    if (a.compareTo(b) <= 0 && a.compareTo(c) <= 0) min = a;
    else if (b.compareTo(a) <= 0 && b.compareTo(c) <= 0) min = b;
    else if (c.compareTo(b) <= 0 && c.compareTo(a) <= 0) min = c;
    return min;

and similarly for get max only slightly different. 同样,对于get max仅略有不同。 How can I go about creating the getMiddle. 我该如何创建getMiddle。 Also we are not allowed to use arrays as we "haven't learned" them yet. 另外,我们还不能使用数组,因为我们还没有“学习”它们。 and the prof said that the code for get middle should be around 5-6 lines. 教授说,获取中间代码应该在5-6行左右。

Thanks 谢谢

Multiply return values of compareTo method. 乘以compareTo方法的返回值。 If the value is middle, results of compareTo method have different signs. 如果值为中间,则compareTo方法的结果具有不同的符号。 do multiply result is zero or has negative sign. 乘积结果为零或具有负号。

String getMiddle(String a,String b,String c)
{
    String middle = "";
    if (a.compareTo(b)*a.compareTo(c) <= 0) middle = a;
    else if (b.compareTo(a)*b.compareTo(c) <= 0) middle = b;
    else if (c.compareTo(b)*c.compareTo(a) <= 0) middle = c;
    return middle;
}
 String middle = "";
    if (a.compareTo(b) <= 0 && a.compareTo(c) >= 0) middle = a;
    else if (b.compareTo(a) <= 0 && b.compareTo(c) >= 0) middle = b;
    else if (c.compareTo(b) <= 0 && c.compareTo(a) >= 0) middle = c;
    return middle;

Doing it your way, it would look like this: 用自己的方式做,看起来像这样:

if      (a.compareTo(b) > 0 && a.compareTo(c) <= 0) middle = a;
else if (a.compareTo(c) > 0 && a.compareTo(b) <= 0) middle = a;
else if (b.compareTo(a) > 0 && b.compareTo(c) <= 0) middle = b;
else if (b.compareTo(c) > 0 && b.compareTo(a) <= 0) middle = b;
else middle = c;
return middle

Well, that's the general gist behind it. 好吧,这就是背后的一般要点。 You could combine some of these together for fewer lines, but I'll leave that up to you. 您可以将其中一些合并在一起以减少行数,但我将由您自己决定。

Why so complicated ? 为什么这么复杂? Just use TreeSet , it uses compareTo() internally :). 只需使用TreeSet ,它就在内部使用compareTo():)。

this gives the mid..haven't tested it thorougly..and also, as for the number of lines prediction, i'm not sure i met it very well...anyway... 这给了中点..没有经过全面测试..而且,关于行数的预测,我不确定我是否很好地满足了...无论如何...

    String mid = "";
    if (a.compareTo(b) <= 0) {
        if (b.compareTo(c) <= 0) mid = b;
        else mid = c;}
    else if(a.compareTo(c) <= 0) mid = a;
    else mid = c; 
    return mid;

我将只使用TreeSet是因为它在添加数据后对数据进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM