2015-02-24 65 views
0

我忘记了使用最简单的方法对4个数字进行排序的代码。我到处搜索这些代码,但仍无法找到它。使用JOptionPane从最小到最大排序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); 
    } 
} 
+2

而问题是...... – MaxZoom 2015-02-24 19:18:32

+1

什么是代码number1-number4至少到最大的排序? sort =(this code); – thecodester 2015-02-24 19:20:40

+0

Arrays.sort()?! – 2015-02-24 19:26:44

回答

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); 
} 
0

您可以使用从阵列现有的库函数排序:

List<Double> list = new ArrayList<>(); 
list.add(n1); list.add(n2); list.add(n3); list.add(n4); 
Arrays.sort(list); 

这里是Arrays文档。