2015-02-24 60 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文檔。