for循環
格式
for(初始化值1;循環終止條件2;循環體執行後的語句4){
循環體3;
}
for (int i = 0; i <= 10; i++) {
System.out.println(i);
}
流程
初始化值1
->循環終止條件2
「true」->循環體3
->循環體執行後的語句4
->循環終止條件2
「true」->循環體3
->循環體執行後的語句4
->…循環終止條件2
「false」-> 循環結束
增強for循環
for(內部元素類型 變量名;集合){}
普通方式實現
int[] arr = {100, 24, 90, 78};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
增強方式實現
優點:代碼少
缺點:不能直接獲取索引
for (int i : arr) {
System.out.println(i);
}
求和
package com.zhongxin.loop;
public class ForDemo2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 10; i++) {
sum += i;
}
System.out.println(sum);
}
}
while循環
package com.zhongxin.loop;
public class WhileDemo {
public static void main(String[] args) {
int i = 1; // 初始化語句1
while (i <= 10) { // 循環執行條件2
System.out.println(i); // 循環體3
i++; // 循環體執行完畢之後的語句4
}
}
}
流程:1234->2(true)->34->2(true)->......->2(false)->循環結束
Do…While
至少會執行一次循環體,其他和while
一樣
package com.zhongxin.loop;
public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}
break
跳出整個循環
for (int i = 1; i <= 10; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
// 1
// 2
continue
結束本次循環,繼續下次循環
package com.zhongxin.loop;
public class BreakContinueDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
// 1
// 2
// 4
// 5
// 6
// 7
// 8
// 9
// 10
條件控制語句
switch
if … else
if else if … else
if … if … if
if
package com.zhongxin.ifdemo;
public class IFDemo {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("恭喜你成年了");
}
if (age >= 18) {
System.out.println("可以訪問本網站");
}else{
System.out.println("FBI WARNING!");
}
int score = 60;
if (score == 100) {
System.out.println("滿分");
}else if (score>=90){
System.out.println("優秀");
}else if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}
switch
優點
效率比if快
結束的條件
switch
右大括號
變量類型
<= int
String
枚舉
package com.zhongxin.ifdemo;
public class SwitchDemo {
public static void main(String[] args) {
int i = 1;
switch (i) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("未知");
break;
}
}
}
不使用break 形成穿透
package com.zhongxin.ifdemo;
public class SwitchDemo2 {
public static void main(String[] args) {
int mouth = 12;
switch (mouth) {
case 3:
case 4:
case 5:
System.out.println("春");
break;
case 6:
case 7:
case 8:
System.out.println("夏");
break;
case 9:
case 10:
case 11:
System.out.println("秋");
break;
case 12:
case 1:
case 2:
System.out.println("冬");
break;
default:
System.out.println("未知");
break;
}
}
}
練習
打印
* ** *** **** *****
package com.zhongxin.ifdemo;
public class Homework {
public static void main(String[] args) {
String start = "*";
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.print(start);
}
System.out.println("");
}
}
}
打印
* * * * * * * * * * * * * * *
package com.zhongxin.ifdemo;
public class Homework1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int x = 1; x <= i * 2 - 1; x += 2) {
System.out.print("* ");
}
System.out.println();
}
}
}