第五章練習題2

Duanhaifeng55發表於2018-04-02

//鍵盤輸入隨機數字的幾組數字,輸出幾組資料中最大的數和最小的數

public static void four() {

int max = 0;
int min = 0;
int num = 0;
Scanner input = new Scanner(System.in);
System.out.println("請輸入一個數值:");
num=min=max=input.nextInt();
do {
if (num > max) {
max = num;
} else if (num < min) {
min = num;
}
System.out.println("請輸入一個數值:");
//Scanner input = new Scanner(System.in);
num = input.nextInt();
} while (num != 0);
System.out.println("最大值是:" + max + "最小值是:" + min);
}
}