我正在嘗試解決此代碼中遇到的兩個(gè)問題。首先,我想將我的整數(shù)輸入設(shè)置為最大值 100,其次我試圖獲取輸入的整數(shù)數(shù)組并按絕對(duì)值對(duì)它們進(jìn)行排序。所以如果用戶使用這個(gè)程序,我希望他們只能輸入 99 來表示他們想要排序的整數(shù)數(shù)量。當(dāng)它最終被排序而不是降序或升序時(shí),我希望它是這樣的;-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])+ " "); } }}
添加回答
舉報(bào)
0/150
提交
取消