分支語句if else
-
格式一
if(條件) 單行語句; else 單行語句;
-
格式二
if(條件){ 多行語句; } else{ 多行語句; }
-
格式三
if(){} else if(){} else if(){} else{ },整體結構,只有一個分支會執行
-
格式四
if(){ if(){} else{} } //嵌套if-else語句
if else 案例
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cc = 100;
if (cc <= 100) {
cc += 50;
if (cc < 100) {
System.out.println("小于100");
} else if (cc < 500) {
System.out.println("小于500");
} else {
System.out.println("大于500");
}
}
//輸入一個星期的數,并輸出對應的星期名稱,0-星期日,1-星期一,。。6-星期六
System.out.println("輸入一個星期的數,0-星期日,1-星期一,...,6-星期六: ");
int wIndex = sc.nextInt();
String wName = "星期";
if (wIndex == 0) {
wName += "日";
} else if (wIndex == 1) {
wName += "一";
} else if (wIndex == 2) {
wName += "二";
} else if (wIndex == 3) {
wName += "三";
} else if (wIndex == 4) {
wName += "四";
} else if (wIndex == 5) {
wName += "五";
} else if (wIndex == 6) {
wName += "六";
} else {
wName = "星期的編號出錯!!!";
}
System.out.println(wName);
}
}
分支語句 switch
用法:
switch(表達式1){ //表達式的數據類型: char,byte,short,int,long,enum(jdk 1.5),String(jdk 1.7)
case 數值1:
case 數值2:
break;
default:
break;
}
上面case語句沒有一個被執行或者在case語句中不存在break,則進入此處,進行默認行為的處理
switch 案例
public class Demo {
public static void main(String[] args) {
switch (10) {
case 5:
System.out.println("表達式與5匹配");
break;
case 10:
System.out.println("表達式與10匹配");
break;
case 15:
System.out.println("表達式與15匹配");
break;
default:
System.out.println("表達式中不存在與10匹配的語句或沒有出現break");
}
switch (5) {
case 5: {
int i = 5; //變量的作用域: 從聲明位置開始到 右大括號結束
System.out.println(i);
//break;
}
case 10:
int i = 10;
System.out.println(i);
break;
case 15:
System.out.println("表達式與15匹配");
break;
default:
System.out.println("表達式中不存在與10匹配的語句或沒有出現break");
}
}
}
循環語句 while do while
語句模式
-
while循環: 先判斷條件,如果條件為true則執行循環體,當循環體執行完后,再接著判斷條件。。。
while(條件表達式){ 循環體; //執行某一重復使用的功能代碼 改變循環條件的值; }
-
do-while循環: 先執行循環體,再判斷條件
do{ }while(條件表達式)
while與do-while的區別:
while是先判斷后執行(可能一次都不執行循環體); do-while先執行,再判斷(至少執行一次循環體)
while、 do while案例
public class Demo {
public static void main(String[] args) {
//要求: 求1~100之間數值的和
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println("sum=" + sum);
//重置變量
i = 1;
sum = 0;
do {
sum += i;
i++;
} while (i <= 100);
System.out.println("sum=" + sum);
//要求: 求5的階乘
int y = 5;
int result = 1;
do {
result *= i;
y--;
} while (y > 1);
System.out.println("5的階乘為 " + result);
}
}
for循環
格式
for(初始化表達式1;循環表達式2;循環條件變量變化表達式3){
//循環體
}
表達式1: 定義循環中使用的變量和循環條件中的變量,其作用域僅限當前for循環中
表達式2: 每次循環的條件表達式,只有條件為true時,才會執行循環體
表達式3: 執行完循環體之后,執行的表達式,一般用于改變循環條件變量的值
嵌套for:
for(;;){
for(;;){
}
}
for 循環案例
public class Demo{
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
//循環100次
System.out.println(i+"");
}
int i = 0;
for (; i < 100; i++) {
//循環100次
}
System.out.println(i); //i的作用域只限于for循環體中,可以將初始化表達式的變量放在for循環外定義
/* for (; ; ) { //死循環
}*/
//如果上面的死循環執行,就不會執行下面的調用方法
System.out.println("hello");
}
}
continue break 的區別

continue break補充
public class Demo {
public static void main(String[] args) {
int k = 1, count = 0;
for (; k <= 100; k++) {
if (k % 2 == 0) {
continue; //結束本次循環,轉到下一次循環
//后面代碼同樣不會執行
}
count++;//有多少個奇數
}
System.out.println(count);
System.out.println("===================================");
//
int sum = 0;
outer:
for (int m = 0; m < 100; m++) {
inner:
for (int n = 0; n < 20; n++) {
sum += n; //sum ++;
if (n == 5) break;//跳出了當前循環,但跳不出第二層循環
if (sum >= 100) {
break outer;//跳出outer
}
}
}
System.out.println(sum);
}
}