问题详情

从高到低将从命令行读取的一组数字进行降序排序。


时间:2021-07-17 17:55 关键词:

答案解析

<p><font face="宋体" size="3"> <br></font></p><p><span>从高到低将从命令行读取的一组数字进行降序排序。</span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri">import java.util.Arrays;</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri">public class ExamDemo {</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>public static void main(String[] args) {</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>int[] a = new int[args.length];</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><font face="Calibri"><span lang="EN-US"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span lang="NO-BOK">int j, temp, i;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></font></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="NO-BOK"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>for (i = 0; i &lt; args.length; i++)<span>&nbsp; </span>a[i] = Integer.parseInt(args[i]);</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="NO-BOK"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>for (i = 0; i &lt; a.length - 1; i++){</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="NO-BOK"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>for (j = 0; j &lt; a.length - i - 1; j++){</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><font face="Calibri"><span lang="NO-BOK"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span lang="EN-US">if (a[j] &lt; a[j + 1]) {</span></font></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>temp = a [j];</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>a[j] = a[j + 1];</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>a[j + 1] = temp;</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>for (i = 0; i &lt; a.length; i++)</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>System.out.print(a[i] + " ");</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>System.out.println();</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri">}</font></span></p><p><font face="宋体" size="3"> <br></font></p>