简体   繁体   English

根据大小对一定数量的用户输入数字进行排序(Java)

[英]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.minMath.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.

相关问题 java,统计用户输入的数字 - java, counting the numbers entered by the user Java 程序从用户那里读取不定数量的数字,直到输入正数 - Java program reading indefinite amount of numbers from a user till a positive is entered 简单的Java App可计算不确定数量的输入数字的平均值 - Simple Java App to calculate the average of an indefinite amount of entered numbers 如何对用户在Java中定义的随机生成的数量进行排序? - How can I sort an amount of randomly generated numbers defined by the user in Java? Java:用户按字母顺序输入的名称选择类别 - Java: Selection sort of names entered by the user in alphabetical order java按用户输入的字符串值对字符串进行排序 - java sort list of strings by string value entered by a user Java. 程序计算用户在数组中输入的 7 个数字的出现次数 - Java. programs counts the occurences of 7 numbers entered by the user in an array (Java) 如何跟踪用户输入的数字以确定最小值和最大值 - (Java) How to Keep Track of Numbers Entered by User for Determining Min and Max 我如何确定用户输入的数字是否包含正确数量的数字和-标记 - How would I determine whether a number entered by a user contains the proper amount of numbers and - marks 如何读取用户输入到具有设定大小的数组中的输入量? - How can I read the amount of input a user has entered into an array with a set size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM