我正在尝试解决此代码中遇到的两个问题。首先,我想将我的整数输入设置为最大值 100,其次我试图获取输入的整数数组并按绝对值对它们进行排序。所以如果用户使用这个程序,我希望他们只能输入 99 来表示他们想要排序的整数数量。当它最终被排序而不是降序或升序时,我希望它是这样的;-1,2,-6,10,-20。 public static void main(String args[]){ int n, i, j, temp; int arr[] = new int[50]; Scanner scan = new Scanner(System.in); //Input area for user data, setting the number of integers to sort System.out.print("Enter Total Number of Integers you would like to sort : "); n = scan.nextInt(); for (n=0, n < 100, n++) { } //Input area for user data, asking user to input the ints into an array for sorting System.out.print("Enter " +n+ " Numbers : "); for(i=0; i<n; i++) { arr[i] = scan.nextInt(); } // Sorting Array using Bubble Sort Technique for(i=0; i<(n-1); i++) { for(j=0; j<(n-i-1); j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } // Command line output from user, sorted by absolute value System.out.print("Sorted List : \n"); for(i=0; i<n; i++) { System.out.print(java.lang.Math.abs(arr[i])+ " "); } }}
添加回答
举报
0/150
提交
取消