目錄
案例:入年齡,假設年齡大於 60 歲,則提示“老年”;如果年齡介於 40 歲至 60 歲之間,則提示“中年”;如果年齡介於 18 歲至 40 歲之間,則提示“少年”; 18 歲以下則提示“童年”
案例:打印輸出12.25的各個位的數字,請輸入一個數字:12.25拆分後的數字:1 2 2 5
案例:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和
二重循環
二重循環概念:即嵌套循環,就是一個循環放在另一個循環內,外面的循環可以稱爲外層循環,嵌套在裏面的循環可以稱爲內層循環
案例:打印出5行5列的*的矩陣
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
案例:打印出下列圖形
*
**
***
****
分析:*隨着行數的增加而增加(+1)
****
***
**
*
分析:*隨着行數的增加而自減(-1)
*
**
***
****
****
***
**
*
案例:入年齡,假設年齡大於 60 歲,則提示“老年”;如果年齡介於 40 歲至 60 歲之間,則提示“中年”;如果年齡介於 18 歲至 40 歲之間,則提示“少年”; 18 歲以下則提示“童年”
int age = 0;// 年齡
String res = null;// 結果
Scanner input = new Scanner(System.in);
System.out.println("請輸入年齡:");
age = input.nextInt();
if (age < 0) {
System.out.println("年齡沒有負數!");
return;
}
if (age >= 60) {// 條件之間是互斥
res = "老年";
} else if (age >= 40) {
res = "中年";
} else if (age >= 18) {
res = "少年";
} else {
res = "童年";
}
System.out.println(age + "歲是" + res);// 數字之間串接不能直接使用+,串聯一個字符串(可以什麼都沒有)
}
案例:打印輸出12.25的各個位的數字,請輸入一個數字:12.25拆分後的數字:1 2 2 5
方法1:
// 解決xx.xx數字獲取它的各個位的數字
double num = 12.25;// 數據源
// String str=num+"";//double轉成String類型
int num_1 = 0, num_2 = 0, num_3 = 0, num_4 = 0;// 各個位
int res = (int) (num * 100);// 化整
num_1 = res / 1000;
num_2 = res % 1000 / 100;
num_3 = res % 100 / 10;
num_4 = res % 10;
System.out.println("拆分後的數字:" + num_1 + " " + num_2 + " " + num_3 + " " + num_4);
方法2:
double d = 24.25;
int i = (int) (d * 100);
int num = 1000;
int res = 0;
while (i > 0) {
res = i / num;
System.out.print(res + " ");
i %= num;
num /= 10;
}
案例:求出1-1000所有的素數
boolean isPrime = true;// 判斷素數
int count = 0;// 計數器
for (int i = 1; i < 1001; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
if (count == 4) {
System.out.println();
count = 0;// 歸位
}
System.out.print(i + "\t");
count++;
}
isPrime = true;// 歸位
}
案例:輸出九九乘法表
for (int i = 1; i <= 9; i++) {
System.out.println("");
for (int j = 1; j <= i; j++) {
System.out.print(i + "x" + j + "=" + j * i + "\t");
}
}
案例:有一分數序列:2/1,3/2,5/3,8/5,13/8,21/13...求出這個數列的前20項之和
double a = 1;//分子
double b = 1;//分母
double fraction = a / b;
double sum = 0;
double a1 = 0;
double b1 = 0;
for (int i = 0; i < 20; i++) {
a1 =a;
b1 = b;
a=a1+b1;//分子
b=a1;//分母
fraction = a / b;
sum += fraction;
System.out.println("第"+(i+1)+"個分數是:"+(int)a+"/"+(int)b);
}
System.out.println("前20個分數總和爲:"+sum);
}
案例:製作萬年曆
public static void main(String[] args) {
// TODO Auto-generated method stub
int year;
int month;
int days = 0;
int totalDays = 0;// 所有天數
boolean isRn;// 瑞年保存true,否則爲false
System.out.println("*************************萬年曆**********************");
Scanner input = new Scanner(System.in);
System.out.print("請輸入年");
year = input.nextInt();
System.out.print("請輸入月");
month = input.nextInt();
// 判斷是否是瑞年
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
isRn = true;
} else {
isRn = false;
}
// 計算年的總天數
for (int i = 1900; i < year; i++) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
totalDays += 366;
} else {
totalDays += 365;
}
}
// 求月份之前的天數和
for (int i = 1; i <= month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (isRn) {
days = 29;
} else {
days = 28;
}
}
if (i != month) {
totalDays += days;
}
}
// 求星期幾
int beforeDays;
beforeDays = 1 + totalDays % 7;
if (beforeDays == 7) {
beforeDays = 0;//
}
System.out.println("星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t");
// 先打印表格
for (int i = 0; i < beforeDays; i++) {
System.out.print("\t");
}
for (int i = 1; i <= days; i++) {
System.out.print(i + "\t");
// 日曆表格滿個換行
if ((i + beforeDays) % 7 == 0) {
System.out.println();
}
}
}