【前言】:本篇主要以代碼舉例的形式來講解順序結構、分支結構、回圈結構以及輸入輸出的方式,最后寫了一個猜數字游戲,并用畫圖的方式簡單說了一下idea的除錯方式,旁觀者清,如發現有錯誤,請及時評論或私信指正,歡迎來訪!!!
文章目錄
- 順序結構
- 分支結構
- if 陳述句
- 代碼示例1:判斷一個數是奇數還是偶數
- 代碼示例2:判段某一年份是否是閏年
- 代碼示例3:判斷一個數字是正數還是負數
- switch陳述句
- 代碼示例4: 根據 day 的值輸出星期
- 回圈結構
- while回圈
- 代碼示例5:列印1~9的數字
- 代碼示例6:計算1~100的和
- 代碼示例7:計算1~100的奇數和,偶數和
- 代碼示例8:計算n的階乘
- 代碼示例9:求1!+2!+3!+4!+5!(1~n)的階乘的和
- break
- 代碼示例10: 找到 100 - 200 中第一個 3 的倍數
- continue
- 代碼示例11: 找到 100 - 200 中所有3 的倍數
- 代碼示例12:找到 1~100之間既能被3整除又能被5整除的數字
- for 回圈
- 代碼示例13:打9印1到9的數字
- 代碼示例14:計算1-100的和
- 代碼示例15:計算1!+2!+3!+4!+5!
- do while回圈
- 輸入輸出
- 輸出到控制臺
- 格式化字串
- 從鍵盤輸入
- Scanner:輸入工具類
- 猜數字游戲
- 程式的除錯
順序結構
按照代碼書寫的執行順序一行一行的執行
如:
System.out.println("aaa");
System.out.println("bbb");
System.out.println("ccc");
// 運行結果
aaa
bbb
ccc
如果調整代碼的書寫順序, 則執行順序也發生變化
System.out.println("aaa");
System.out.println("ccc");
System.out.println("bbb");
// 運行結果
aaa
ccc
bbb
分支結構
if 陳述句
基本語法形式1:單分支
if(布爾運算式){
//條件滿足時執行代碼
}
代碼示例:
public static void main(String[] args) {
int a = 10;
if (a == 10) {
System.out.println(a);
}
}
基本語法形式2:雙分支
if(布爾運算式){
//條件滿足時執行代碼
}else{
//條件不滿足時執行代碼
}
代碼示例1:判斷一個數是奇數還是偶數
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);//System.in:從鍵盤獲取資料
int a = scanner.nextInt();//輸入一個整數
System.out.println(a);//將你輸入的整數輸出
if (a % 2 == 0) {
System.out.println("a是偶數");
} else {
System.out.println("a是奇數");
}
}
1).Scanner scanner = new Scanner();輸入一個整數的固定寫法,使用Scanner需要導包(import java.util.Scanner),他這個包一般都會自動匯入,這個包類似于C語言的include
2).scanner:是我們自己設定的變數
3)System.in:從鍵盤獲取資料
4)不知道Scanner在哪個包底下怎么辦?查幫助手冊
代碼示例2:判段某一年份是否是閏年
public static void main4(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();//輸入一個整數
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("這一年是閏年");
} else {
System.out.println("這一年不是閏年");
}
}
閏年
普通閏年:公歷年份是4的倍數的,且不是100的倍數(year % 4 == 0 && year % 100 != 0 ),為普通閏年(如2004年、2020年就是閏年),
世紀閏年:公歷年份是整百數的,必須是400的倍數(year % 400 == 0)才是世紀閏年(如1900年不是世紀閏年,2000年是世紀閏年),
基本語法形式3:多分支
if(布爾運算式){
//條件滿足時執行代碼
}else if(布爾運算式){
//條件滿足時執行代碼
}else{
//條件都不滿足時執行代碼
}
代碼示例3:判斷一個數字是正數還是負數
public static void main3(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();//輸入一個整數
if (num > 0) {
System.out.println("num為正數");
} else if (num < 0) {
System.out.println("num為負數");
} else {
System.out.println("num既不是正數也不是負數");
}
}
注意事項: 懸垂 else 問題
public static void main5(String[] args) {
//if...else陳述句不加大括號只能寫一條陳述句,建議加上大括號
int x = 10;
int y = 10;
if (x == 10)
if (y == 10)
System.out.println("aaa");
else
System.out.println("ccc");
}
運行結果:
1)結論:else只會和與它最接近的if匹配
2)為避免懸垂 else 問題出現:建議加上大括號,if…else陳述句不加大括號只能寫一條陳述句,
3)注意:java代碼風格是將左大括號與if/else 放在同一行
switch陳述句
基本語法
switch(整數|列舉|字符|字串){
case 內容1 : {
內容滿足時執行陳述句;
[break;]
}
case 內容2 : {
內容滿足時執行陳述句;
[break;]
}
...
default:{
內容都不滿足時執行陳述句;
[break;]
}
}
代碼示例:
public static void main6(String[] args) {
// int a=1;
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
switch (a) {//a是switch的引數,型別是int
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2);
break;
default:
System.out.println("輸入有誤");
break;
}
}
代碼示例4: 根據 day 的值輸出星期
public static void main6(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)在Java語言當中不能做switch的引數的資料型別有哪些?
long、float、double、boolean
2)不寫 break 的時候, case 陳述句會依次向下執行, 從而失去了多分支的效果.
3)switch中的值只能是整數、列舉、字符、字串
switch 不能表達復雜的條件如:(num > 10 && num < 20)
回圈結構
while回圈
基本語法格式:
while(回圈條件){
回圈陳述句;
}
回圈條件為true,則執行回圈陳述句,否則結束回圈
代碼示例5:列印1~9的數字
public static void main(String[] args) {
int num = 1;
while (num != 10) {
System.out.println(num);
num++;
}
}
代碼示例6:計算1~100的和
public static void main10(String[] args) {
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println(sum);
}
代碼示例7:計算1~100的奇數和,偶數和
public static void main11(String[] args) {
int sumOdd = 0;//1~100奇數和
int sumEve = 0;//1~100偶數和
int i = 1;
while (i <= 100) {
sumOdd += i;
i += 2;
}
System.out.println(sumOdd);
i = 2;//改變i的初值
while (i <= 100) {
sumEve += i;
i += 2;
}
System.out.println(sumEve);
}
列印結果:
代碼示例8:計算n的階乘
public static void main12(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//輸入一個整數
int ret = 1;
int i = 1;
while (i <= n) {
ret *= i;
i++;
}
System.out.println(ret);
}
運行結果:
代碼示例9:求1!+2!+3!+4!+5!(1~n)的階乘的和
public static void main13(String[] args) {
Scanner scanner = new Scanner(System.in);//System.in:從鍵盤獲取數值
int n = scanner.nextInt();
int sum = 0;
int j = 1;
// 外層回圈負責求階乘的和
while (j <= n) {//回圈是可以嵌套的
int ret = 1;
int i = 1;
// 里層回圈負責完成求階乘的細節.
while (i <= j) {
ret *= i;
i++;
}
sum = sum + ret;
j++;
}
System.out.println(sum);
}
列印結果:
注意事項
1).和 if 類似, while 下面的陳述句可以不寫 { } , 但是不寫的時候只能支持一條陳述句. 建議還是加上 { }
2).和 if 類似, while 后面的 { 建議和 while 寫在同一行.
3).和 if 類似, while 后面不要多寫 分號, 否則可能導致回圈不能正確執行
break
break 的功能是讓回圈提前結束.
代碼示例:
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 6) {
break;//break結束所有回圈
}
System.out.println(i);
i++;
}
}
代碼示例10: 找到 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++;
}
}
列印結果:
continue
continue 的功能是跳過這次回圈, 立即進入下次回圈.
代碼示例11: 找到 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++;
}
}
執行到 continue 陳述句的時候, 就會立刻進入下次回圈(判定回圈條件), 從而不會執行到下方的列印陳述句
代碼示例12:找到 1~100之間既能被3整除又能被5整除的數字
//方法1:
public static void main16(String[] args) {
int i = 1;
while (i <= 100) {
if (i % 3 != 0 || i % 5 != 0) {
i++;
continue;
}
System.out.println(i);
i++;
}
}
//方法2:使用最小公倍數
public static void main17(String[] args) {
int i = 1;
while (i <= 100) {
if (i % 15 != 0) {
i++;
continue;
}
System.out.println(i);
i++;
}
}
列印結果:
for 回圈
基本語法
for(運算式1;運算式2;運算式3){ //運算式2一定是一個布爾運算式
回圈體;
}
//運算式1: 用于初始化回圈變數.
//運算式2: 回圈條件
//運算式3: 更新回圈變數.
//相比于 while 回圈, for 回圈將這三個部分合并在一起, 寫代碼時不容易遺漏.
代碼示例13:打9印1到9的數字
public static void main18(String[] args) {
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
代碼示例14:計算1-100的和
public static void main19(String[] args) {
int i=0;
int sum=0;
for(i=1;i<=100;i++){
sum+=i;
}
System.out.println(sum);
}
代碼示例15:計算1!+2!+3!+4!+5!
public static void main21(String[] args) {
int sum=0;
int i=1;
//第一個for回圈是產生1到5的數字
for(i=1;i<=5;i++){
//第二個for回圈是求一個數的階乘
int ret=1;
for(int j=1;j<=i;j++){//
ret*=j;
}
sum+=ret;//求和
}
System.out.println("result="+sum);
}
運行結果:
do while回圈
基本語法
do{
回圈陳述句;
}while(回圈條件);
//先執行回圈陳述句, 再判定回圈條件.
代碼示例:
public static void main(String[] args) {
int i=0;
do{
System.out.println("至少執行一次:"+"hehe haha");
}while(i!=0);
}
列印結果:
注意:
do…while回圈:至少會執行一次
do while 回圈最后的分號不要忘記
輸入輸出
輸出到控制臺
基本語法
System.out.println(msg); // 輸出一個字串, 帶換行
System.out.print(msg); // 輸出一個字串, 不帶換行
System.out.printf(format, msg); // 格式化輸出
代碼示例:
public static void main23(String[] args) {
String msg="hello";
String format="world";
System.out.println(msg); // 輸出一個字串, 帶換行
int a=10;
System.out.printf("a=%d\n",a);
System.out.print(msg); // 輸出一個字串, 不帶換行
System.out.printf(format, msg); // 格式化輸出
}
列印結果:
格式化字串
| 轉換符 | 型別 | 舉例 | |
|---|---|---|---|
| d | 十進制整數 | ("%d", 100) | 100 |
| x | 十六進制整數 | ("%x", 100) | 64 |
| o | 八進制整數 | ("%o", 100) | 144 |
| f | 定點浮點數 | ("%f", 100f) | 100.000000 |
| e | 指數浮點數 | ("%e", 100f) | 1.000000e+02 |
| g | 通用浮點數 | ("%g", 100f) | 100.000 |
| a | 十六進制浮點數 | ("%a", 100) | 0x1.9p6 |
| s | 字串 | ("%s", 100) | 100 |
| c | 字符 | ("%c", 1) | 1 |
| b | 布林值 | ("%b", 1) | true |
| h | 散列碼 | ("%h", 100) | 64 |
| % | 百分號 | ("%d.2f%%", 2/7f) | 0.29% |
從鍵盤輸入
讀入一個字符 (選學)
public static void main24(String[] args) {
//《》
System.out.println("輸入一個字符:");
char i= 0;//read報錯:選到它,按住Alt+回車鍵,選中第二個,捕獲例外
//這種太復雜,不建議使用
try {
i =(char)System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你輸入的字符是:"+i);
}
Scanner:輸入工具類
public static void main25(String[] args) {
Scanner scanner=new Scanner(System.in);
String str2=scanner.nextLine();
//這里的nextLine在讀入的時候遇到空格不結束,但它需要放到最上面
System.out.println(str2);
byte b=scanner.nextByte();
System.out.println(b);
float f=scanner.nextFloat();
System.out.println(f);
String str1=scanner.next();//這里的next在讀入的時候,遇到空格結束,
System.out.println(str1);
}
運行結果:
注意:如果輸入與型別不匹配時會報錯

用Scanner回圈讀入n個數字不結束,想要結束使用ctrl+D
public static void main26(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextInt()){
int a=scanner.nextInt();
System.out.println(a);
}
}
運行結果:
猜數字游戲
游戲規則:
系統自動生成一個隨機整數(1-100), 然后由用戶輸入一個猜測的數字. 如果輸入的數字比該亂數小, 提示 “低 了”,
如果輸入的數字比該亂數大, 提示 “高了” , 如果輸入的數字和亂數相等, 則提示 “猜對了” .
public static void main28(String[] args) {
Scanner scanner=new Scanner(System.in);
//電腦隨機生成數字
Random random=new Random(22);
int rand=random.nextInt(100)+1;//bound:100表示生成[0,100),需要+1生成1~100的數
while(true) {
System.out.println("請輸入數字:");
int num=scanner.nextInt();
if (num < rand) {
System.out.println("低了");
} else if (num > rand) {
System.out.println("高了");
} else {
System.out.println("等于");
break;
}
}
}
}
運行結果:
程式的除錯
畫圖解說:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292806.html
標籤:其他
上一篇:HarmonyOS實戰:基于鴻蒙服務卡片的分布式游戲
下一篇:漢諾塔(C語言實作)

