1 順序語句
語句:使用分号分隔的代碼稱作為一個語句。

注意:沒有寫任何代碼隻是一個分号的時候,也是一條語句,稱作空語句。
順序語句就是按照從上往下的順序執行的語句。
2 判斷(if…else)
在我們找工作的過程中,要求兩年工作經驗以上且年齡超過30歲。
什麼是判斷語句:用于判斷的語句叫判斷語句。
1.格式一
if(判斷條件){
如果符合條件執行的代碼;
執行的代碼塊1;
執行的代碼塊2;
……………….;
執行的代碼塊n;
}
練習:提示使用者輸入一個整數。如果該整數是5的倍數,列印“5的倍數”如果是2的倍數列印“2的倍數”
提示:為了便于讓使用者輸入資料,我們使用Scanner這個類,固定用法Scanner sc=new Scanner(System.in); 該類需要導入包import java.util.Scanner;
int nextInt = sc.nextInt();擷取使用者輸入的數字
import java.util.Scanner;
public class Demo9 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int nextInt = sc.nextInt();
if(nextInt%5==0){
System.out.println("是5的倍數");
}
if(nextInt%2==0){
System.out.println("是2的倍數");
}
}
}
2.格式二
if(判斷條件){
執行的代碼塊1;
執行的代碼塊2;
……………….;
執行的代碼塊n;
}else{
執行的代碼塊1;
執行的代碼塊2;
……………….;
執行的代碼塊n;
}
案例:判斷一個整數是奇數還是偶數
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個整數:");
int nextInt = sc.nextInt();
if (nextInt % 2 == 0) {
System.out.println("是偶數");
} else {
System.out.println("是奇數");
}
System.out.println("over");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個整數:");
int nextInt = sc.nextInt();
if (nextInt % 2 == 0) {
System.out.println("是偶數");
} else {
System.out.println("是奇數");
}
System.out.println("over");
}
同樣道理如果花括号中隻有一條語句,那麼花括号可以省略不寫,初學者不推薦省略。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一個整數:");
int nextInt = sc.nextInt();
if (nextInt % 2 == 0)
System.out.println("是偶數");
else
System.out.println("是奇數");
System.out.println("over");
}
觀察發現if else語句有點類似于三元運算符.其實三元運算符是if else 的一種簡寫格式.
Public static void main(String[] args) {
int x = 0, y = 1, b;
// if else 語句
if (x > y) {
b = x;
} else {
b = y;
}
System.out.println(b);// 1
// 3元運算
b = x > y ? x : y;
System.out.println(b); // 1
}
這兩種格式是一樣的。if else 結構 簡寫格式: 變量 = (條件表達式)?表達式1:表達式2;
三元運算符:
好處:可以簡化if else代碼。
弊端:因為是一個運算符,是以運算完必須要有一個結果。
3. 格式三
if(判斷條件1){
執行的代碼塊1;
}else if(判斷條件2){
執行語句;
}else if(判斷條件3){
執行語句;
}
需求: 根據使用者定義的數值不同,列印對應的星期英文。if 隻能進行一層判斷,if else 隻能進行兩層判斷,那麼需要多層判斷時呢?星期可是有7個數的。如何設計代碼?
使用if 語句
public static void main(String[] args) {
int x = 8;
if (x == 1) {
System.out.println("星期一");
}
if (x == 2) {
System.out.println("星期二");
}
if (x == 3) {
System.out.println("星期三");
}
}
如果這樣設計的話,第一個if語句執行完畢後,第二個語句仍會執行(去判斷),是一個順序結構.那麼事實上目前定義的星期之後會有一個.假如,第一個已經符合條件,那麼剩餘的執行就沒有意義了。屬于邏輯錯誤。
使用if else ,如果使用者輸入的是7以外的資料,那麼怎麼處理?就需要使用else 了
方案2:使用if else if語句
public static void main(String[] args) {
int x = 8;
if (x == 1) {
System.out.println("星期一");
} else if (x == 2) {
System.out.println("星期二");
} else if (x == 3) {
System.out.println("星期三");
} else if (x == 4) {
System.out.println("星期四");
} else if (x == 5) {
System.out.println("星期五");
} else if (x == 6) {
System.out.println("星期六");
} else if (x == 7) {
System.out.println("星期日");
} else {
System.out.println("請輸入數字1-7");
}
}
注意:
public static void main(String[] args) {
int x = 5;
if (x == 1) {
System.out.println("1");
}
if (x == 2) {
System.out.println("2");
}
if (x == 3) {
System.out.println("3");
} else {
System.out.println("4"); // 4
}
}
該if 語句不是一個整體,第一個if 是一個語句,第二個又是一個語句,最後的if else 又是一個語句。
if語句特點
第二種格式與三元運算符的差別:三元運算符運算完要有值出現。好處是:可以寫在其他表達式中。
條件表達式無論寫成什麼樣子,隻看最終的結構是否是true 或者 false。
練習1: 根據使用者輸入的月份,列印出月份所屬的季節.
練習2: 根據使用者輸入的成績,進行評級,根據學生考試成績劃分ABCD
練習1:
public static void main(String[] args) {
int x = 1;
if (x == 3) {
System.out.println("spring");
} else if (x == 4) {
System.out.println("spring");
}
}
仔細觀察:發現if和else if要執行的語句是一樣的,可不可以合并呢。當然是可以的。怎麼合并?使用邏輯運算符,那麼使用哪個邏輯運算符呢, &肯定不行。需要全部為真才為真,月份是不可能同時滿足的 那麼使用|連接配接符号即可。意思隻要其中一個為真,就為真。另外可以使用短路功能。
public static void main(String[] args) {
int x = 1;
if (x == 3 || x == 4 || x == 5) {
System.out.println("spring");
} else if (x == 6 || x == 7 || x == 8) {
System.out.println("Summer");
} else if (x == 9 || x == 10 || x == 11) {
System.out.println("autumn");
} else {
System.out.println("Winter");
} else {
System.out.println("月份不存在");
}
}
練習2:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入考試分數:");
double score = sc.nextDouble();
char grade;
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
System.out.println("你的成績是:" + grade);
}
If語句常見的錯誤:
1.忘記必要的括号:如果代碼塊中隻有一條語句的時候,可以省略花括号,但是當花括号将多條語句擴在一起時,花括号就不能在省略。
double radius = 4;
double area;
if (radius >= 0)
area = radius * radius * 3.14;
System.out.println("The area " + " is " + area);
double radius = 4;
double area;
if (radius >= 0) {
area = radius * radius * 3.14;
System.out.println("The area " + " is " + area);
}
雖然代碼一樣多,但是第一個會編譯報錯(area沒有出初始化),第二個正常運作。就是因為少了花括号。是以一定要仔細。
2.if語句後出現分号
double radius = 0;
double area;
if (radius > 0); {
area = radius * radius * 3.14;
System.out.println("The area " + " is " + area);
}
判斷閏年
1:什麼是閏年?可以被4整除不能被100整除,或者可以被400整除,那麼這一年就是閏年(leap year)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入年份:");
int year = sc.nextInt();
// 判斷年份能否被4整除
boolean isLeapYear = (year % 4 == 0);
// 年份能被4整除,并且不能被100整除并且使用&&(and)
isLeapYear = isLeapYear && (year % 100 != 0);
// 年份或者能夠被400整除
isLeapYear = isLeapYear || (year % 400 == 0);
if (isLeapYear) {
System.out.println(year + "是閏年!");
}
// 簡寫格式;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "是閏年!");
}
}
3 選擇判斷語句(switch)
switch語句
格式:
switch(表達式)
{
case 取值1:
執行語句;
break;
case 取值2:
執行語句;
break;
…...
default:
執行語句;
break;
}
switch語句特點:
1,switch語句選擇的類型隻有四種:byte,short,int , char。
2,case之間與default沒有順序。先判斷所有的case,沒有比對的case執行
default。
3,switch語句停止的條件是遇到了break關鍵字或者結束switch語句的大括号。
4,如果比對的case或者default沒有對應的break,那麼程式會繼續向下執行,運
行可以執行的語句,直到遇到break或者switch結尾結束。
5,switch case中的值必須要與switch表達式的值具有相同的資料類型。而且case後跟的值必須是常量,不能跟變量。
public static void main(String[] args) {
int x = 3;
switch (x) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
case 3:
System.out.println("3");
break;
default:
System.out.println("ok");
break;
}
}
case 就像選擇題的答案之一。 break 就是如果該答案正确那麼就可以跳出switch 了,意思就是說 已經找出了正确的答案了。那麼這道題也就做完了。如果 case 沒有比對接着進行下一個case 比對,直到比對為止。 最後如果都沒有比對上,那麼 switch 給提供了一個預設的答案,就是 default。
注意: case後跟的是冒号:
每個case中的執行語句一定要加break;
練習:
需求2:根據用于指定的月份,列印該月份所屬的季節.
一旦case比對,就會順序執行後面的程式代碼,而不管後面的case是否比對,直到遇見break,利用這一特性可以讓好幾個case執行統一語句.
345 spring 678 sunmer 9 10 11 autumn 12 1 2 winter
public static void main(String[] args) {
int x = 3;
switch (x) {
case 3:
case 4:
case 5:
System.out.println("spring");
break;
case 6:
case 7:
case 8:
System.out.println("sunmer");
break;
case 9:
case 10:
case 11:
System.out.println("autumn");
break;
case 12:
case 0:
case 1:
System.out.println("winter");
default:
System.out.println("ok");
break;
}
}
練習:char 類型在switch 中的使用.
public static void main(String[] args) {
int x = 1, y = 2;
char ch = '*';
switch (ch) {
case '+':
System.out.println("x*y=" + (x + y));
break;
case '-':
System.out.println("x-y="+(x-y));
break;
case '*':
System.out.println("x*y="+(x*y));
break;
case '/':
System.out.println("x/y="+(x/y));
break;
default:
System.out.println("不靠譜");
}
}
if 和switch 語句很像,具體什麼場景下,應用哪個語句呢?
如果判斷的具體數值不多,而是符号byte,short int char 四種類型.
雖然2個語句都可以使用,建議使用switch語句.因為效率稍高.
其他情況:
對區間判斷,對結果為boolean 類型判斷,使用if if的使用範圍更廣。
if 除了能判斷具體數值還能判斷區間。switch 判斷區間會很費勁的。要寫好多case 對于運算結果是boolean型的 if 能判斷 switch 是不能實作的。例如:根據學生考試成績劃分ABCD A90-100 B80-89 C70-79 D60-69 E0-59。
實際開發怎麼選擇呢?
如果要對具體數值進行判斷,并且數值不多,那麼 就用switch 來完成。switch的case條件都是編譯期整數常量,編譯器可以做到表格跳轉查詢,查找速度快。
但是switch 的局限性比較大必須是4種類型,并且值不多。一般都是使用if。 最後在jdk 7中對switch 進行了增強 還可以判斷字元串。5.0 增加了對枚舉的判斷。
備注:JDK7.0開始可以使用switch可以使用字元串類型的資料了.
4 While循環
需求:需要列印一行字元串"hello gzitcast",100次
就需要将該語句列印100遍System.out.println(“hello gzitcast”);
那麼如何解決該問題?
Java提供個一個稱之為循環的結構,用來控制一個操作的重複執行。
int count = 0;
while (count < 100) {
System.out.println("hello gzitcast");
count++;
}
System.out.println("over");
變量count初始化值為0,循環檢查count<100 是否為true,如果為true執行循環體(while後{}之間的語句),輸出"hello gzitcast"語句,然後count自增一,重複循環,直到count是100時,也就是count<100為false時,循環停止。執行循環之後的下一條語句。
Java提供了三種類型的循環語句:while循環,do-while循環和for循環。
1、while語句格式:
while(條件表達式)
{
執行語句;
}
定義需求: 想要列印5次helloworld
public static void main(String[] args) {
System.out.println("hello world");
System.out.println("hello world");
System.out.println("hello world");
System.out.println("hello world");
System.out.println("hello world");
}
2、
public static void main(String[] args) {
int x = 0;
while (x < 5) {
System.out.println("hello java ");
}
}
如果是在dos裡編譯和運作,是不會停止,除非系統當機。需要ctrl+c來結束。
這就是真循環或者死循環。因為x<5 永遠為真。
public static void main(String[] args) {
int x = 0;
while (x < 5) {
System.out.println("hello java ");
x++;
}
}
讓x自增,那麼就會有不滿足條件的時候。循環就會結束。
練習:想要列印出1-100之間的奇數
public static void main(String[] args) {
int x = 1;
while (x < 100) {
System.out.println(x);
x = x + 2;
}
}
public static void main(String[] args){
int x=1;
while(x<100){
if(x%2!=0){
System.out.print(x);
}
x++;
}
System.out.println();
}
練習2:計算1+2+3+4+5+6+7+8+9 的值
int sum = 0;
int i = 1;
while (i < 10) {
sum = sum + i;
i++;
}
System.out.println(sum);
注意:要精确控制循環的次數。常犯錯誤是是循環多執行一次或者少執行一次。
例如會執行101次,想要執行100次,要麼是count初始值為1,然後count<=100
要麼是count初始值為0,coung<100
int count = 0;
while (count <=100) {
System.out.println("hello gzitcast");
count++;
}
System.out.println("over");
猜數字遊戲:
編寫程式随即生成一個0-100之間的随機數。程式提示使用者輸入一個數字,不停猜測,直到猜對為止。最後輸出猜測的數字,和猜測的次數。并且如果沒有猜中要提示使用者輸入的值是大了還是小了。
思考:
如何生成1-100之間随機數?
(int)(Math.random()*100)+1;
如何提示使用者輸入數字,
Scanner sc=new Scanner(System.in);
int guessNum = sc.nextInt();
需要将随機數和使用者輸入的數字進行比較。
猜一次:
Scanner sc = new Scanner(System.in);
int num = (int)(Math.random()*100)+1;
System.out.println("請輸入0-100之間整數");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} else if (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
這個程式隻能才一次,如何讓使用者重複輸入直到猜對?
可以使用while循環
public static void main(String[] args) {
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("請輸入1-100之間整數");
int guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} else if (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
}
該方案發現了問題,雖然實作了讓使用者不停的輸入,但是即使猜中了程式也不會停止。
那麼就需要控制循環次數了。也就是while() 括号中的條件表達式。當使用者猜測的數和系統生成的數字不相等時,就需要繼續循環。
int num = (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
int guessNum = -1;
while (guessNum != num) {
System.out.println("請輸入1-100之間整數");
guessNum = sc.nextInt();
if (guessNum == num) {
System.out.println("中啦");
} else if (guessNum < num) {
System.out.println("小啦");
} else {
System.out.println("大了");
}
}
為什麼将guessNum初始化值為-1?因為如果初始化為0到100之間程式會出錯,因為可能是要猜的數。
1:首先程式生成了一個随機數
2:使用者輸入一個數字
3:循環檢查使用者數字和随機數是否相同,知道相同位置,循環結束
5 do while 語句
do while語句格式:
do
{
執行語句;
}while(條件表達式);
do while特點是條件無論是否滿足,
循環體至少被執行一次。
public static void main(String[] args) {
int x = 0, y = 0;
do {
System.out.println(x);
x++;
} while (x < 0);
// do while do會先執行一次,不管是否滿足循環條件。
while (y < 0) {
System.out.println(y);
y++;
}
}
while:先判斷條件,隻有條件滿足才執行循環體。
do while: 先執行循環體,再判斷條件,條件滿足,再繼續執行循環體。
簡單一句話:do while:無論條件是否滿足,循環體至少執行一次。
注意一個細節do while 後面的分号;
案例:改寫猜數字遊戲
public static void main(String[] args) {
// 記錄使用者輸入的數字
int guess = -1;
// 記錄使用者輸入次數
int count = 0;
// 生成1-100之間随機數
int num = (int) (int)(Math.random()*100)+1;
Scanner sc = new Scanner(System.in);
// 循環猜數字
do {
System.out.println("請輸入1-100之間的數字");
guess = sc.nextInt();
if (guess > num) {
System.out.println("哥們,太大了");
} else if (guess < num) {
System.out.println("哥們,太小了");
} else {
System.out.println("恭喜,中啦");
}
count++;
} while (num != guess);
System.out.println("你猜測的數字是:" + num + "猜測了" + count + "次");
}
案例:電腦
系統自動生成2個随機數用于參與運算。
系統生成0-4之間的随機數,表示加減乘除取模運算。
使用switch 進行比對
class Couter {
public static void main(String[] args) throws InterruptedException {
// 生成随機數Math.random()生成0-1值,不包含0和1,
//乘以10得到0和10之間的數(double類型),不包含0和10
//強轉為int,并加1得到1和10之間的數,包含1和10
int x = (int)(Math.random()*10)+1;
int y = (int)(Math.random()*10)+1;
System.out.println(x);
System.out.println(y);
// 建立0-4随機數 0 1 2 3 4 各表示加減乘除取模
int z = (int) (int)(Math.random()*5);
System.out.println(z);
switch (z) {
case 0:
System.out.println(x + "+" + y + "=?");
System.out.println("哥們快猜。。。。");
Thread.sleep(2000);
System.out.println(x + "+" + y + "=" + (x + y));
break;
case 1:
System.out.println(x + "-" + y + "=?");
System.out.println("哥們快猜。。。。");
Thread.sleep(2000);
System.out.println(x + "-" + y + "=" + (x - y));
break;
case 2:
System.out.println(x + "*" + y + "=?");
System.out.println("哥們快猜。。。。");
Thread.sleep(2000);
System.out.println(x + "*" + y + "=" + (x * y));
break;
case 3:
System.out.println(x + "/" + y + "=?");
System.out.println("哥們快猜。。。。");
Thread.sleep(2000);
System.out.println(x + "/" + y + "=" + (x / y));
break;
case 4:
System.out.println(x + "%" + y + "=?");
System.out.println("哥們快猜。。。。");
Thread.sleep(2000);
System.out.println(x + "%" + y + "=" + (x % y));
break;
}
}
}
電腦2:上述中隻能計算一次。可以使用whileu循環來不停計算。
程式生成了3個随機數,前兩個數參與運算,第三個數用于比對運算符。要注意除數為0的情況。
int x = (int)(Math.random()*10)+1;
Math.random() 生成0-1之間的數字,double類型
Math.random()*10 就是0-9之間的數,是double類型
(int)(Math.random()*10)将double類型強轉成int類型,去掉小數點,便于計算。
(int)(Math.random()*10)+1,生成了1到10之間随機數。
int z = (int) (int)(Math.random()*5);
生成0-4之間的數字,可以用0表示加,1表示減,2表示乘,3表示除,4表示取模
為了減慢程式,使用了Thread.sleep(2000); 讓程式等待一會。
6 for 循環
1.格式:
for(初始化表達式;循環條件表達式;循環後的操作表達式)
{
執行語句;
}
2.定義需求: 想要列印5次helloworld
public static void main(String[] args) {
for (int x = 0; x < 5; x++) {
System.out.println("hello java");
}
}
3.for的執行流程
for 知道要進行循環,讀到x=0 的時候,在記憶體中開辟了空間,定義變量x 指派為0。接着進行條件判斷 x<5,為真,這個時候對滿足條件後執行了循環體的内容System.out.println(“hello java”);當循環體執行完畢之後,執行x < 5;後的表達式即 x++ 。x自增後變為了1 ,再次進行判斷 x<5 (int x=0 隻執行一次),如果為真就再次運作System.out.println(“hello java”);如果為假,for循環結束。
2、for 和while的差別
public static void main(String[] args) {
for (int x = 0; x < 5; x++) {
System.out.println("hello java");
}
System.out.println(x);
//x cannot be resolved to a variable
int y = 0;
while (y < 5) {
System.out.println("hello world");
y++;
}
System.out.println(y);
錯誤
解釋 x 為什麼會找不到,注意了變量的作用域,也就是變量的作用範圍。x 隻在 for 循環的大括号内有效,出了這個區域,就無效了.在記憶體中就消失了。x消失後,仍要通路它,肯定會報錯的。
y 就不一樣了,y 是定義在while 外的。while循環完畢仍有效 while的初始化 動作在外邊,循環結束後y 仍然存在。
當定義的y 隻作為循環增量存在的話的,循環完畢後y就沒有用了,但是y還是占着一塊記憶體。是以,如果定義的變量隻作為循環增量存在的話,就用for 循環可以節約記憶體。
其實for 和while 是可以互換的。
最後總結
1、for裡面的兩個表達式運作的順序,初始化表達式隻讀一次,判斷循環條件,為真就執行循環體,然後再執行循環後的操作表達式,接着繼續判斷循環條件,重複找個過程,直到條件不滿足為止。
2、while與for可以互換,差別在于for為了循環而定義的變量在for循環結束時就在記憶體中釋放。而while循環使用的變量在循環結束後還可以繼續使用。
3、最簡單無限循環格式:while(true) , for(;?,無限循環存在的原因是并不知道循環多少次,而是根據某些條件,來控制循環。推薦使用while(true)
while(true){
}
for( ; ; ){
}
for(;true;){
}
for 練習:
- 擷取1-10的和,并列印。
- 1-100之間 7的倍數的個數,并列印。
public static void main(String[] args) {
// 擷取1到10的和1+2+3+4+5+6+7+8+9+10
int sum = 0;
for (int x = 1; x <= 10; x++) {
System.out.println((sum + x) + "=" + sum + "+" + x);
sum = sum + x;
}
System.out.println(sum);// 55
}
public static void main(String[] args) {
// 1-100之間 7的倍數的個數,并列印。
int count = 0;
for (int x = 0; x <= 100; x++) {
if (x % 7 == 0) {
System.out.println(x);
count++;
}
}
System.out.println(count);
}
累加思想:通過變量記錄住循環操作後的結果;通過循環的形式.進行累加的動作。
計數器思想:通過一個變量記錄住資料的狀态變化,也是通過循環完成。
循環常見錯誤:
多加分号:在for括号後和循環體之間加分号是常見錯誤。
錯誤:
程式編譯運作都可以通過,隻是不是我們想要的結果。
for(int i=0;i<100;i++);{
System.out.println("hello ");
}
正确:
for(int i=0;i<100;i++){
System.out.println("hello ");
}
錯誤;是一個死循環
int i=0;
while(i<100);{
System.out.println("hello");
i++;
}
int i=0;
while(i<100){
System.out.println("hello");
i++;
}
語句的嵌套應用
什麼是嵌套形式,其實就是語句中還有語句。
想要列印出矩形:
public static void main(String[] args) {
for (int x = 0; x < 5; x++) {
System.out.println("*");
}
}
public static void main(String[] args) {
for (int x = 0; x < 5; x++) {
System.out.print("*");
}
}
這裡用“*”表示矩形的邊。
public static void main(String[] args) {
for (int x = 0; x < 5; x++) {
for(int y=0;y<6;y++){
System.out.print("*");
}
System.out.println();
}
}
forfor 嵌套for循環練習2
列印此種格式的圖案
****
***
**
*
public static void main(String[] args) {
for (int x = 5; x > 0; x--) {
for(int y=x;y>0;y--){
System.out.print("*");
}
System.out.println("");
}
}
練習:
*
**
public static void main(String[] args) {
for (int x = 0; x < 5; x++) {
for (int y = 0; y <= x; y++) {
System.out.print("*");
}
System.out.println("");
}
}
練習:99乘法表
public static void main(String[] args) {
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "*" + x + "=" + x * y + '\t');
}
System.out.println(" ");
}
}
7 break、continue關鍵字
break關鍵字:break 語句用于終止最近的封閉循環或它所在的 switch 語句。控制傳遞給終止語句後面的語句(如果有的話)。
适用:for循環 、 switch兩種循環語句。
break的用法:
- 單獨使用。
- 與标簽一起使用。(标簽:即一個名字,滿足辨別符的條件即可)。
使用細節: 不要再break語句之後,編寫其他語句,永遠都執行不到,編譯報錯。
continue關鍵字:語句将控制權傳遞給它所在的封閉疊代語句的下一次疊代。(跳出本循環,執行下一次循環)。
适用于:while 、 do while 、 for循環語句
使用細節:
1. 如果continue出現在循環的末尾(最後一條語句),那麼可以省略。
2. 如果continue出現在循環的第一條語句,那麼後面的語句都無法執行,是以編譯報錯。
3. 可以結合标記使用。