回覆列表
-
1 # CoreCode
-
2 # IT極客老兵
public static void main(String[] args) {
System.out.println("請輸入整數陣列(逗號分隔):");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine().toString();
String arr[] = str.split(",");
int length = arr.length;
int[] integers = new int[length];
for (int i = 0; i < length - 1; i++) {
integers[i] = Integer.parseInt(arr[i]);
}
Arrays.sort(integers);
System.out.println("min value: " + integers[0]);
System.out.println("max value: " + integers[integers.length - 1]);
System.out.println(Arrays.toString(integers));
scanner.close();
}
程式碼如下
public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入元素個數:"); int n=Integer.parseInt(sc.nextLine()); int[] scores=new int[n]; for(int i=0; i < n; i++) { System.out.println("請輸入整數:"); scores[i]=Integer.parseInt(sc.nextLine());; } Arrays.sort(scores);//從小到大排序 for(int i=0; i < n; i++) { System.out.println(scores[i]); } System.out.println("最小值:" + scores[0]); System.out.println("最大值:" + scores[n-1]); }