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

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

最佳答案

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

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

关于java - 数组的升序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20135585/

10-15 13:40