循環:防止代碼冗餘、可讀性差、容易出錯
#pragma mark-----while循環
// 條件表達式爲真,執行循環體
// while (<#condition#>) {
// <#statements#>
// }
//條件始終成立的循環:死循環, 應該避免出現誤操作的死循環
//輸出30次我很棒
// int i = 0;
//
// while (i < 30) {
//
// printf("i = %d", i);
// printf("我很棒\n");
// i++;
//
// }
//輸出100次helloworld
//count循環變量:控制循環的次數
// int count = 0;
//
// //()內稱爲循環條件 結果只有兩個: 真、假
// while (count < 100) {
//
// printf("count = %d\n", count);
//
// printf("hello world\n");
//
// //循環增量控制
// count++;
// }
// int count = 1;
//
// while (count <= 10) {
// printf("%d ", count++);
// }
//練習:輸出95 到 80之間的數
// int count = 95;
//
// while (count >= 80) {
// printf("%d ", count--);
// }
// 用while打印出 1~100之間7的倍數
// int count = 1;
//
// while (count <= 100) {
//
// if (count % 7 == 0) {
// printf("%d ", count);
// }
//
// count++;
// }
// 用while打印出 1~100之間個位爲7的數。
// int count = 1;
//
// while (count <= 100) {
//
// if ((count - 7) % 10 == 0) {
//
// printf("%d ", count);
// }
//
// count++;
// }
// 用while打印出 1~100之間十位爲7的數。
// 用while打印出 1~100之間不是7的倍數並且不包含7的數
// int count = 1;
//
// while (count <= 100) {
//
// if (count % 7 != 0 && (count % 10 != 7) && (count / 10 != 7)) {
// printf("%d ", count);
//
// }
// count++;
// }
//練習:計算1~5之間所有數的和
int count = 1;
int sum = 0;
while (count <= 5) {
sum = sum + count;//sum += count;
count++;
}
printf("sum = %d\n", sum);
//隨機數 arc4random() 返回一個隨機數,沒有範圍限制,是整數
//如果要隨即一個【a,b】範圍內的數
//公式:arc4random() % (b - a + 1 ) + a
//獲取一個[0, n]之間的隨機數
// 公式:arc4random() % (n + 1) 原理:餘數 < 除數
//獲取【0,10】之間的隨機數
// int number = arc4random() % 11;
// printf("number = %d\n", number);
//使用while循環實現:輸出10個【0,20】之間的隨機數
// int count = 0;
//
// while (count < 10) {
//
// int number = arc4random() % 21;
// printf("%d ", number);
//
// count++;
// }
//練習:使用while輸出N個【0,43】範圍內的隨機數,循環次數從控制檯輸入
// int n = 0;
// int count = 0;
//
// printf("輸入循環次數:");
// scanf("%d", &n);
//
// while (count < n) {
//
//// int number = arc4random() % 44;
//// printf("%d ", number);
// printf("%d ", arc4random() % 44);
//
// count++;
// }
//獲取【a,b】之間的隨機數
int n = 0;
int count = 0;
int max = 0; // 存放臨時最大值
printf("輸入打印個數:");
scanf("%d", &n);
while (count < n) {
//求隨機數 30~70 0~40+30
int number = arc4random() % 41 + 30;
printf("%d ", number);
//比較判斷當前的隨機數是否是最大值
if (max < number) {
max = number;
}
count++;
}
printf("\n");