我忘记了使用最简单方法对4个数字进行排序的代码。我到处搜索这段代码,我仍然无法找到它。
这是我到目前为止所做的:
import javax.swing.JOptionPane;
public class SortingNumbers
{
public static void main(String[] args)
{
String input;
double number1, number2, number3, number4, sort;
int lowest, middle1, middle2, highest
input = JOptionPane.showInputDialog("Enter first number");
number1 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter second numebr");
number2 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter third number");
number3 = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter fourth number");
number4 = Double.parseDouble(input);
JOptionPane.showMessageDialog(null, sort);
System.exit(0);
}
}
答案 0 :(得分:2)
如果您想快速简便地对数字进行排序,我建议将值存储在适当的数组中,并调用Arrays.sort();
例如:
// create the array and put values in it
Double[] x = new Double[4];
x[0] = number1;
x[1] = number2;
x[2] = number3;
x[3] = number4;
// sort the values lowest -> highest
Arrays.sort(x);
// print out each value (but really, you can do anything here)
for (Double y : x) {
System.out.println(y);
}
答案 1 :(得分:0)
您可以使用Arrays中现有的库函数排序:
List<Double> list = new ArrayList<>();
list.add(n1); list.add(n2); list.add(n3); list.add(n4);
Arrays.sort(list);
以下是Arrays文档。