Java,按字母顺序排序字符串,不带数组

编程入门 行业动态 更新时间:2024-10-11 21:26:11
本文介绍了Java,按字母顺序排序字符串,不带数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我有另一个任务做,任务是使用compareTo方法按字母顺序分类3个字符串。基本上,程序从测试器类接收3个字符串(a,b和c),并且它应该返回getMin,getMiddle和getMax。

So I have another assignment to do and the task is to assort 3 strings alphabetically using the compareTo method. 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".

我想出了getmin和max,看起来很容易,但是我有getMiddle的问题。 这是我对最小和最大的:

I figured out the getmin and max, seemed easy but im having problems with the getMiddle. this is what i have for min and max:

String min = ""; if (apareTo(b) <= 0 && apareTo(c) <= 0) min = a; else if (bpareTo(a) <= 0 && bpareTo(c) <= 0) min = b; else if (cpareTo(b) <= 0 && cpareTo(a) <= 0) min = c; return min;

,类似地,get max只是略有不同。我如何去创建getMiddle。此外,我们不允许使用数组,因为我们还没有学习他们。

and similarly for get max only slightly different. How can I go about creating the 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.

感谢

推荐答案

将compareTo方法的返回值相乘。 如果值为中,compareTo方法的结果有不同的符号。 使乘法结果为零或具有负号。

Multiply return values of compareTo method. If the value is middle, results of compareTo method have different signs. do multiply result is zero or has negative sign.

String getMiddle(String a,String b,String c) { String middle = ""; if (apareTo(b)*apareTo(c) <= 0) middle = a; else if (bpareTo(a)*bpareTo(c) <= 0) middle = b; else if (cpareTo(b)*cpareTo(a) <= 0) middle = c; return middle; }

更多推荐

Java,按字母顺序排序字符串,不带数组

本文发布于:2023-11-10 14:21:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   不带   字符串   字母   顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!