LMS知識社群歷程檔案Login
Position: 陳柔禎 > 未分類
檢核3
by 陳柔禎 2019-10-08 13:41:09, Reply(0), Views(40)
package teat;
import java.util.Arrays;
import java.util.Random;

public class HW2 {

public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[4];
for (int i=0; i<4; i++){
arr[i] = r.nextInt(9);
for (int j=0; j<i;){
if (arr[j]==arr[i]){
arr[i] = r.nextInt(9);
j=0;
}
else j++;
}
}
System.out.print("Random : ");
for (int i=0; i<4; i++)
System.out.print(arr[i]+" ");
Arrays.sort(arr);
System.out.println();
int min=0;
int max=0;
int ans=0;
for (int i=0; i<4; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
max = max+arr[0];
    max = max+arr[1]*10;
    max = max+arr[2]*100;
    max = max+arr[3]*1000;
    
    min = min+arr[3];
    min = min+arr[2]*10;
    min = min+arr[1]*100;
    min = min+arr[0]*1000;
    
    ans = max-min;
    System.out.println(max);
    System.out.println(min);
    System.out.println(ans);
}
}

Reply