[英]Sort an amount of user entered numbers according to size (Java)
I'm trying to figure out how to sort a user defined number of user defined numbers according to size, the user defined numbers are doubles. 我试图弄清楚如何根据大小对用户定义的数字进行排序,用户定义的数字为双精度。
I'm trying to do it without using arrays or anything too complex, ideally using some form or combination of Math.min
and Math.max
我正在尝试不使用数组或任何过于复杂的方法来完成此操作,最好使用Math.min
和Math.max
某种形式或组合
eg 例如
int lowestNumber = (int)Math.min(firstNumber, (Math.min(secondNumber, Math.min(thirdNumber, finalNumber) )));
This gets me the lowest number, that's fine, but when i try and do 这可以让我得到最低的数字,这很好,但是当我尝试做时
int secondLowestNumber = (int)Math.min(lowestNumber, firstNumber,(Math.min(secondNumber, Math.min(thirdNumber, finalNumber))));
I get the lowest number again. 我再次得到最低的数字。 I guess the problem is that I don't know how to eliminate the lowest number once I've completed the first assignment. 我想问题是我完成第一次作业后不知道如何消除最低的数字。
Just to summarize the comments on your original question: 只是为了总结对原始问题的评论:
You can sort using an ArrayList very easily using Collections.sort(). 您可以使用Collections.sort()非常容易地使用ArrayList进行排序。 Here's an example: 这是一个例子:
//lets say we have these three numbers:
int num1 = 2, num2 = 5, num3 = 3;
List<Integer> list = new ArrayList<Integer>();
list.add(num1);
list.add(num2);
list.add(num3);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
The above results in the output: 上面的结果输出:
[2, 5, 3]
[2, 3, 5]
As expected! 如预期!
Note we can generalize this using for loops to add more numbers or use other primitive numbers than ints... Let me know if you need other details! 请注意,我们可以使用for循环将其推广为整数,以添加更多数字或使用其他原始数字(而不是整数)。如果需要其他详细信息,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.