if語句的語法格式1:
if(比較表達式) {
語句體;
}
它的執行流程:
1.先計算比較表達式的值,看其返回值是true還是false。
2.如果是true,就執行語句體;
3.如果是false,就不執行語句體;
下面通過代碼來演示if語句:
public class IfDemo01 {
public static void main(String[] args) {
int x = 10;
if(x == 10) {
System.out.println("x等於10");
}
if(x == 20) {
System.out.println("x等於20");
}
System.out.println("over");
}
}
我們在使用if語句的時候需要注意什么呢?
1.比較表達式無論簡單還是復雜,結果必須是boolean類型
2.if語句控制的語句體如果是一條語句,大括號可以省略;如果是多條語句,就不能省略。建議永遠不要省略。
3.一般來說:有左大括號就沒有分號,有分號就沒有左大括號
如下代碼:
public class IfDemo02 {
public static void main(String[] args) {
int x = 10;
if(x == 10) {
System.out.println("x等於10");
}
if((x > 5) || (x == 10)) {
System.out.println("x大於或者等於10");
}
System.out.println("-------------------");
int a = 100;
/*
if(a == 100) {
System.out.println("a的值是100");
}
*/
if(a != 100) {
System.out.println("a的值是100");
System.out.println("over");
}
System.out.println("-------------------");
int b = 100;
if(b != 100); //這里其實是有語句體的,只不過是空語句體。
//代碼塊
{
System.out.println("b的值是100");
System.out.println("over");
}
}
}
if語句的語法格式2:
if(比較表達式) {
語句體1;
}else {
語句體2;
}
它的執行流程:
1.首先計算比較表達式的值,看其返回值是true還是false。
2.如果是true,就執行語句體1;
3.如果是false,就執行語句體2;
我們需要注意的是:else后面是沒有比較表達式的,只有if后面有。
如下我的代碼:
public class IfDemo03 {
public static void main(String[] args) {
//判斷兩個數據是否相等
int a = 10;
int b = 20;
if(a == b) {
System.out.println("a等於b");
}else {
System.out.println("a不等於b");
}
}
}
if語句的語法格式3:
if(比較表達式1) {
語句體1;
}else if(比較表達式2) {
語句體2;
}else if(比較表達式3) {
語句體3;
}
...
else {
語句體n+1;
}
它的執行流程:
1.首先計算比較表達式1看其返回值是true還是false,
2.如果是true,就執行語句體1,if語句結束。
3.如果是false,接着計算比較表達式2看其返回值是true還是false,如果是true,就執行語句體2,if語句結束。
4.如果是false,接着計算比較表達式3看其返回值是true還是false,…如果都是false,就執行語句體n+1。
下面通過案例演示if語句語法3,如下:
案例:鍵盤錄入一個成績,判斷並輸出成績的等級。
import java.util.Scanner;
public class IfDemo05 {
public static void main(String[] args) {
//需求:鍵盤錄入一個成績,判斷並輸出成績的等級。
/*
90-100 優秀
80-90 好
70-80 良
60-70 及格
0-60 不及格
*/
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
//錄入數據
System.out.println("請輸入你的考試成績:");
int score = sc.nextInt();
/*
if(score>=90 && score<=100) {
System.out.println("優秀");
}else if(score>=80 && score<90) {
System.out.println("好");
}else if(score>=70 && score<80) {
System.out.println("良");
}else if(score>=60 && score<70) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
*/
//這樣寫已經滿足我的基本要求,但是可能別人在使用的時候,不會按照你要求的數據給出了。
//在做一個程序的基本測試的時候,一定要考慮這樣的幾個問題:
//正確數據,錯誤數據,邊界數據。
//而我們剛才寫的程序並沒有處理錯誤數據,所以這個程序不是很好,要改進
/*
if(score>=90 && score<=100) {
System.out.println("優秀");
}else if(score>=80 && score<90) {
System.out.println("好");
}else if(score>=70 && score<80) {
System.out.println("良");
}else if(score>=60 && score<70) {
System.out.println("及格");
}else if(score>=0 && score<60){
System.out.println("不及格");
}else {
System.out.println("你輸入的成績有誤");
}
*/
//另一種判斷改進
if(score<0 || score>100) {
System.out.println("你輸入的成績有誤");
}else if(score>=90 && score<=100) {
System.out.println("優秀");
}else if(score>=80 && score<90) {
System.out.println("好");
}else if(score>=70 && score<80) {
System.out.println("良");
}else if(score>=60 && score<70) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}