流程控制語句
順序結構 先後順序,依次執行
public static void main(String[] args){
System.out.println(“第一步”);
System.out.prinktln(“第二步”);
System.out.println(“最後一步”);
}
}
選擇結構
選擇結構也被稱爲分支結構,選擇結構有特定的語法規則,代碼要執行具體的邏輯運算進行判斷,邏輯運算的結果有兩個,所以產生選擇,按照不同的選擇執行不同的代碼
if語句
接下來通過一段代碼,學習一下if語句的具體用法,IfDemo01.java
public class IfDemo01 {
public static void main(String[] args) {
int x = 5;
if (x < 10) {
x++;
}
System.out.println("x=" + x);
}
}
if…else語句
接下來通過一段代碼,來實現判斷奇偶數的程序,IfDemo02.java
public class IfDemo02 {
public static void main(String[] args) {
int num = 19;
if (num % 2 == 0) {
// 判斷條件成立,num被2整除
System.out.println("num是一個偶數");
} else {
System.out.println("num是一個奇數");
}
}
}
if…else if…else語句
接下來通過一段代碼,來實現對學生考試成績進行等級劃分的程序,
從鍵盤錄入一個成績進行判斷,根據要求打印結果:
成績在[0,60)打印該成績爲差
成績在[60,70)打印該成爲中
成績在[70,80)打印該成績爲良
成績在[80,100]打印該成績爲優
public class IFDemo {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int score=scan.nextInt();
if(score>=0 && score<60){
System.out.println("打印成績爲差");
}else if(score>=60 && score<70){
System.out.println("打印成績爲中");
}else if(score>=70 && score<80){
System.out.println("打印成績爲良");
}else if(score>=80 && score<=100){
System.out.println("打印成績爲優");
}
}
}
選擇結構switch
接下來通過一個案例演示根據數字來輸出中文格式的星期,如下所示。SwitchDemo01.java
public class SwitchDemo01 {
public static void main(String[] args) {
int week = 5;
switch (week) {
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;
}
}
}
循環語句while
接下來通過一段代碼,來實現打印1~4之間的自然數,WhileDemo.java
public class WhileDemo {
public static void main(String[] args) {
int x = 1; // 定義變量x,初始值爲1
while (x <= 4) { // 循環條件
System.out.println("x = " + x); // 條件成立,打印x的值
x++; // x進行自增
}
}
}
循環語句for
接下來通過一個案例對自然數1~4進行求和,如下所示。ForDemo01.java
public class ForDemo01 {
public static void main(String[] args) {
int sum = 0; // 定義變量sum,用於記住累加的和
for (int i = 1; i <= 4; i++) { // i的值會在1~4之間變化
sum += i; // 實現sum與i的累加
}
System.out.println("sum = " + sum); // 打印累加的和
}
}
for循環案例for循環實現1-5之間數據求和
public class ForTest2 {
public static void main(String[] args) {
//定義求和變量,初始化值是0
int sum = 0;
//獲取1-5之間的數據,用for循環實現
for(int x=1; x<=5; x++) {
//把每一次獲取到的數據,累加起來就可以了
//sum = sum + x;
/*
* 第一次:sum = 0 + 1 = 1
* 第二次:sum = 1 + 2 = 3
* 第三次:sum = 3 + 3 = 6
* 第四次:sum = 6 + 4 = 10
* 第五次:sum = 10 + 5 = 15
*/
sum += x;
}
//輸出求和結果
System.out.println("sum:"+sum);
}
}
跳轉語句(break、continue)
當break出現在循環語句中,作用是跳出循環語句,執行後面的代碼,接下來通過下面一段代碼,實現將當變量x的值爲3時,使用break語句跳出循環,代碼如下所示。BreakDemo.java
public class BreakDemo {
public static void main(String[] args) {
int x = 1; // 定義變量x,初始值爲1
while (x <= 4) { // 循環條件
System.out.println("x = " + x); // 條件成立,打印x的值
if (x == 3) {
break;
}
x++; // x進行自增
}
}
}
continue語句用在循環語句中,它的作用是終止本次循環,執行下一次循環。接下來通過一個練習對1~100之內的奇數求和,ContinueDemo.java
public class ContinueDemo {
public static void main(String[] args) {
int sum = 0; // 定義變量sum,用於記住和
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) { // i是一個偶數,不累加
continue; // 結束本次循環
}
sum += i; // 實現sum和i的累加
}
System.out.println("sum = " + sum);
}
}