
目錄
一,寫在前面
二,順序結構
三,分支結構
1,if 陳述句
2,switch 陳述句
三,回圈結構
1,while 回圈
2,break
3,continue
4,for 回圈
四,輸入輸出
1,輸出到控制臺
2,從鍵盤輸入
五,基礎練習
1,年齡分析
2,判斷一個數是否為素數
3,輸出 1000 - 2000 之間所有的閏年
4,輸出乘法口訣表
5,求兩個正整數的最大公約數
6,計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,
一,寫在前面
如果你對Java感興趣,那么你可以關注我這個小博主,你信任我,我也會提升我博客的質量,最好是保姆級教學,保證就算你是小白看過我的博客后,你也能學的懂,當然你最起碼要懂一點最基本的編程語言知識,如果你認為本篇博客寫的不錯的話,求點贊,求評論,求收藏,
二,順序結構
順序結構比較簡單. 像我們之前寫過的代碼就是順序結構的, 按照代碼書寫的順序一行一行執行,
public static void main(String[] args) {
System.out.println("aaa");
System.out.println("bbb");
System.out.println("ccc");
}

如果調整代碼的書寫順序, 則執行順序也發生變化
public static void main(String[] args) {
System.out.println("aaa");
System.out.println("ccc");
System.out.println("bbb");
}

三,分支結構
1,if 陳述句
基本語法形式1
if(布爾運算式){
//條件滿足時執行代碼
}
基本語法形式2
if(布爾運算式){
//條件滿足時執行代碼
}else{
//條件不滿足時執行代碼
}
基本語法形式3
if(布爾運算式){
//條件滿足時執行代碼
}else if(布爾運算式){
//條件滿足時執行代碼
}else{
//條件都不滿足時執行代碼
}
代碼示例1: 判定一個數字是奇數還是偶數
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0) {
System.out.println("num 是偶數");
} else {
System.out.println("num 是奇數");
}
}

代碼示例2: 判定一個數字是正數還是負數
public static void main(String[] args) {
int num = 10;
if (num > 0) {
System.out.println("num 是正數");
} else if (num < 0) {
System.out.println("num 是負數");
} else {
System.out.println("num 是 0");
}
}

代碼示例3: 判定某一年份是否是閏年
public static void main(String[] args) {
int year = 2000;
if (year % 100 == 0) {
// 判定世紀閏年
if (year % 400 == 0) {
System.out.println("是閏年");
} else {
System.out.println("不是閏年");
}
} else {
// 普通閏年
if (year % 4 == 0) {
System.out.println("是閏年");
} else {
System.out.println("不是閏年");
}
}
}

懸垂 else 問題
public static void main(String[] args) {
int x = 10;
int y = 10;
if (x == 10)
if (y == 10)
System.out.println("aaa");
else
System.out.println("bbb");
}
//書寫代碼時,盡量在if和else加括號

2,switch 陳述句
基本語法
switch(整數|列舉|字符|字串){
case 內容1 : {
內容滿足時執行陳述句;
[break;]
}
case 內容2 : {
內容滿足時執行陳述句;
[break;]
}
...
default:{
內容都不滿足時執行陳述句;
[break;]
}
}
代碼示例: 根據 day 的值輸出星期
public static void main(String[] args) {
int day = 1;
switch(day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("輸入有誤");
break;
}
}

根據 switch 中值的不同, 會執行對應的 case 陳述句. 遇到 break 就會結束該 case 陳述句. 如果 switch 中的值沒有匹配的 case, 就會執行 default 中的陳述句. 我們建議一個 switch 陳述句最好都要帶上 default,
注意事項1 break 不要遺漏, 否則會失去 "多分支選擇" 的效果
public static void main(String[] args) {
int day = 1;
switch(day) {
case 1:
System.out.println("星期一");
// break;
case 2:
System.out.println("星期二");
break;
}
}

注意事項2 switch 中的值只能是 整數|列舉|字符|字串
public static void main(String[] args) {
double num = 1.0;
switch(num) {
case 1.0:
System.out.println("hehe");
break;
case 2.0:
System.out.println("haha");
break;
}
}

注意事項3 switch 不能表達復雜的條件
/ 例如: 如果 num 的值在 10 到 20 之間, 就列印 hehe
// 這樣的代碼使用 if 很容易表達, 但是使用 switch 就無法表示.
if (num > 10 && num < 20) {
System.out.println("hehe");
}
三,回圈結構
1,while 回圈
基本語法格式:
while(回圈條件){
回圈陳述句;
}
代碼示例1: 列印 1 - 10 的數字
public static void main(String[] args) {
int num = 1;
while (num <= 10) {
System.out.print(" "+num);
num++;
}
}

代碼示例2: 計算 1 - 100 的和
public static void main(String[] args) {
int n = 1;
int result = 0;
while (n <= 100) {
result += n;
n++;
}
System.out.println(result);
}

代碼示例3: 計算 5 的階乘
public static void main(String[] args) {
int n = 1;
int result = 1;
while (n <= 5) {
result *= n;
n++;
}
System.out.println(result);
}

代碼示例4: 計算 1! + 2! + 3! + 4! + 5!
public static void main(String[] args) {
int num = 1;
int sum = 0;
// 外層回圈負責求階乘的和
while (num <= 5) {
int factorResult = 1;
int tmp = 1;
// 里層回圈負責完成求階乘的細節.
while (tmp <= num) {
factorResult *= tmp;
tmp++;
}
sum += factorResult;
num++;
}
System.out.println("sum = " + sum);
}

注意事項
1. 和 if 類似, while 下面的陳述句可以不寫 { } , 但是不寫的時候只能支持一條陳述句. 建議還是加上 { }
2. 和 if 類似, while 后面的 { 建議和 while 寫在同一行.
3. 和 if 類似, while 后面不要多寫 分號, 否則可能導致回圈不能正確執行.
int num = 1;
while (num <= 10); {
System.out.println(num);
num++;
}
// 執行結果
[無任何輸出, 程式死回圈]
2,break
break 的功能是讓回圈提前結束,
代碼示例: 找到 100 - 200 中第一個 3 的倍數
public static void main(String[] args) {
int num = 100;
while (num <= 200) {
if (num % 3 == 0) {
System.out.println("找到了 3 的倍數, 為:" + num);
break;
}
num++;
}
}

3,continue
continue 的功能是跳過這次回圈, 立即進入下次回圈
代碼示例: 找到 100 - 200 中所有3 的倍數
public static void main(String[] args) {
int num = 100;
while (num <= 200) {
if (num % 3 != 0) {
num++; // 這里的 ++ 不要忘記! 否則會死回圈.
continue;
}
System.out.println("找到了 3 的倍數, 為:" + num);
num++;
}
}

4,for 回圈
基本語法
for(運算式1;運算式2;運算式3){
回圈體;
}
運算式1: 用于初始化回圈變數.
運算式2: 回圈條件
運算式3: 更新回圈變數
代碼示例1: 列印 1 - 10 的數字
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
代碼示例2: 計算 1 - 100 的和
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("sum = " + sum);
// 執行結果
5050
代碼示例3: 計算 5 的階乘
int result = 0;
for (int i = 1; i <= 5; i++) {
result *= i;
}
System.out.println("result = " + result);
代碼示例4: 計算 1! + 2! + 3! + 4! + 5!
int sum = 0;
for (int i = 1; i <= 5; i++) {
int tmp = 1;
for (int j = 1; j <= i; j++) {
tmp *= j;
}
sum += tmp;
}
System.out.println("sum = " + sum);
注意事項 (和while回圈類似)
1. 和 if 類似, for 下面的陳述句可以不寫 { } , 但是不寫的時候只能支持一條陳述句. 建議還是加上 { }
2. 和 if 類似, for 后面的 { 建議和 while 寫在同一行.
3. 和 if 類似, for 后面不要多寫 分號, 否則可能導致回圈不能正確執行.
四,輸入輸出
1,輸出到控制臺
基本語法
public static void main(String[] args) {
System.out.println("msg"); // 輸出一個字串, 帶換行
System.out.print("msg"); // 輸出一個字串, 不帶換行
System.out.printf("format, msg"); // 格式化輸出
}

println 輸出的內容自帶 \n,
print 不帶 \n printf 的格式化輸出方式和 C 語言的 printf 是基本一致的
格式化字串
| 轉換符 | 型別 | 舉例 | |
| d | 十進制整數 | ("%d", 100) | 100 |
| x | 十六進制整數 | ("%x", 100) | 64 |
| f | 定點浮點數 | ("%f", 100) | 100.000000 |
| e | 指數浮點數 | ("%e", 100) | 1.000000e+02 |
| g | 通用浮點數 | ("%g", 100) | 100.000 |
| a | 十六進制浮點數 | ("%a", 100) | 0x1.9p6 |
| s | 字串 | ("%s", 100) | 100 |
| c | 字符 | ("%c", 1) | 1 |
| b | 布林值 | ("%b", 100) | true |
| h | 散列碼 | ("%h", 100) | 64 |
| o | 八進制整數 | ("%o", 100) | 144 |
| % | 百分號 | ("%.2%%", 2/7f) | 0.29% |
2,從鍵盤輸入
使用 Scanner 讀取字串/整數/浮點數
import java.util.Scanner; // 需要匯入 util 包
public class fsfa {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的姓名:");
String name = sc.nextLine();
System.out.println("請輸入你的年齡:");
int age = sc.nextInt();
System.out.println("請輸入你的工資:");
float salary = sc.nextFloat();
System.out.println("你的資訊如下:");
System.out.println("姓名: "+name+"\n"+"年齡:"+age+"\n"+"工資:"+salary);
sc.close(); // 注意, 要記得呼叫關閉方法
}
}

使用 Scanner 回圈讀取 N 個數字
import java.util.Scanner; // 需要匯入 util 包
public class fsfa {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0.0;
int num = 0;
while (sc.hasNextDouble()) {
double tmp = sc.nextDouble();
sum += tmp;
num++;
}
System.out.println("sum = " + sum);
System.out.println("avg = " + sum / num);
sc.close();
}
}

五,基礎練習
1,年齡分析
根據年齡, 來列印出當前年齡的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if(n > 0 && n < 18) {
System.out.println("少年");
}else if(n >= 18 && n < 29) {
System.out.println("青年");
}else if(n >= 29 && n < 56) {
System.out.println("中年");
}else {
System.out.println("老年");
}
}

2,判斷一個數是否為素數
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int i = 2;
for (; i < n; i++) {
if(n % i == 0) {
System.out.println(n+" 不是素數!");
break;
}
}
//2種情況
if( i == n) {
System.out.println(n + " 是素數!");
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int i = 2;
for (; i <= n/2; i++) {
if(n % i == 0) {
System.out.println(n+" 不是素數!");
break;
}
}
//2種情況
if( i > n/2) {
System.out.println(n + " 是素數!");
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int i = 2;
for (; i <= Math.sqrt(n); i++) {
if(n % i == 0) {
System.out.println(n+" 不是素數!");
break;
}
}
//2種情況
if( i > Math.sqrt(n)) {
System.out.println(n + " 是素數!");
}
}

3,輸出 1000 - 2000 之間所有的閏年
public static void main(String[] args) {
for (int year = 1000; year < 2000; year++) {
/*if(year % 100 !=0) {
if(year % 4 == 0) {
System.out.println(year+" 是閏年!");
}
}else {
if(year % 400 == 0) {
System.out.println(year+" 是閏年!");
}
}*/
if(year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {
System.out.println(year+" 是閏年!");
}
}
}

4,輸出乘法口訣表
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i ; j++) {
System.out.print(i+"*"+j+"="+i*j+" ");//\t
}
System.out.println();
}
}

5,求兩個正整數的最大公約數
public static void main(String[] args) {
int a = 24;
int b = 18;
int c = a%b;//24 % 18 = 6
while (c != 0) {
a = b;//a = 18
b = c;//b = 6
c = a%b;//0
}
System.out.println("最大公約數是"+b);
}

6,計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,
public static void main(String[] args) {
double sum = 0.0;
int flg = 1;
for (int i = 1; i <= 100 ; i++) {
sum = sum + 1.0/i * flg;
flg = -flg;
}
System.out.println(sum);
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328055.html
標籤:其他
上一篇:在專欄中失去分鐘
下一篇:CSV資料框列顯示不正確的類名
