2013-11-22 75 views
0

醜陋的代碼我知道,但我需要輸出顯示按升序輸入的數字。由於它只寫入兩個輸入顯示在輸出中......任何人都可以幫忙嗎?陣列升序

for(int i=1;i<=2;i++) // declares i as an integer and sets it to 1; do while i<=2, increment i 
{        
    if (numarray[i] < result0) // is numarray[1] < numarray[0] 
    result0 = numarray[i];//yes sets numarray[1] = result0      
    else 
    result1 = numarray[i]; //no sets result numarray[1] =result1 

    if (numarray[i] > result1) // is numarray[2] < result1 
    result2 = numarray[i]; // set numarray[2] = result1 

    if(numarray[i]< result0) //if numarray[2] < result0 
    result0 = numarray[i]; //yes sets numarray[2] = result0 
    else 
    result1 = numarray[i]; //no sets numarray[2] = result2       
} 
System.out.println("The numbers from smallest to largest is:" + result0 + result1 + result2); 

回答

1

您可以排序並使用此代碼打印您陣列:

Array.sort(yourArray); 
System.out.println(Arrays.toString(yourArray)); 
0

你可以嘗試使用排序方法從陣列類的Java:

Arrays.sort(numarray); //Ascending 
Arrays.sort(numarray, Collections.reverseOrder()); //Descending