数组的升序

时间:2013-11-22 01:58:00

标签: java arrays integer

丑陋的代码我知道,但我需要输出来显示按升序输入的数字。在编写时,输出中只显示两个输入......任何人都可以帮忙吗?

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

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码对数组进行排序和打印:

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

答案 1 :(得分:0)

您可以尝试使用Java中 Arrays 类的排序方法:

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