第三天內(nèi)容 if語句 及其練習(xí)

package com.itheima_02;

/*

* if語句有三種格式。

*

* if語句格式1:

* if(關(guān)系表達(dá)式) {

* 語句體;

* }

*

* 執(zhí)行流程:

* A:首先判斷關(guān)系表達(dá)式看其結(jié)果是true還是false

* B:如果是true,就執(zhí)行語句體

* C:如果是false,就不執(zhí)行語句體

*/

public class IfDemo {

public static void main(String[] args) {

System.out.println("開始");

// 定義兩個變量

int a = 10;

int b = 20;

if (a == b) {

System.out.println("a等于b");

}

int c = 10;

if (a == c) {

System.out.println("a等于c");

}

System.out.println("結(jié)束");

}

}



ackage com.itheima_02;

/*

* if語句格式2:

* if(關(guān)系表達(dá)式) {

* 語句體1;

* }else {

* 語句體2;

* }

*

* 執(zhí)行流程:

* A:判斷關(guān)系表達(dá)式的值是true還是false

* B:如果是true,就執(zhí)行語句體1

* C:如果是false,就執(zhí)行語句體2

*/

public class IfDemo2 {

public static void main(String[] args) {

System.out.println("開始");

// 判斷給定的數(shù)據(jù)是奇數(shù)還是偶數(shù)

// 定義變量

int a = 100;

// 給a重新賦值

a = 99;

if (a % 2 == 0) {

System.out.println("a是偶數(shù)");

} else {

System.out.println("a是奇數(shù)");

}

System.out.println("結(jié)束");

}

}

ackage com.itheima_02;

/*

* if語句格式3:

* if(關(guān)系表達(dá)式1) {

* 語句體1;

* }else if(關(guān)系表達(dá)式2) {

* 語句體2;

* }else if(關(guān)系表達(dá)式3) {

* 語句體3;

* }

* ...

* else {

* 語句體n+1;

* }

*

* 執(zhí)行流程:

* A:首先判斷關(guān)系表達(dá)式1看其結(jié)果是true還是false

* B:如果是true,就執(zhí)行語句體1

* ? ? 如果是false,就繼續(xù)進(jìn)行關(guān)系表達(dá)式2的判斷看其結(jié)果是true還是false

* C:如果是true,就執(zhí)行語句體2

* ? 如果是false,就繼續(xù)進(jìn)行關(guān)系表達(dá)式...的判斷看其結(jié)果是true還是false

* ...

* D:如果沒有一個為true的,就執(zhí)行語句體n+1

*

* if語句的三種格式:

* 第一種格式適合做一種情況的判斷

* 第二種格式適合做二種情況的判斷

* 第三種格式適合做多種情況的判斷

*/

public class IfDemo3 {

public static void main(String[] args) {

// x和y的關(guān)系滿足如下:

// x>=3 y = 2x + 1;

// -1<=x<3 y = 2x;

// x<=-1 y = 2x – 1;

// 根據(jù)給定的x的值,計算出y的值并輸出。

// 定義變量

int x = 5;

/*

int y;

if (x >= 3) {

y = 2 * x + 1;

} else if (x >= -1 && x < 3) {

y = 2 * x;

} else if (x <= -1) {

y = 2 * x - 1;

}else {

y = 0;

}

*/

int y = 0;

if (x >= 3) {

y = 2 * x + 1;

} else if (x >= -1 && x < 3) {

y = 2 * x;

} else if (x <= -1) {

y = 2 * x - 1;

}

System.out.println("y的值是:"+y);

}

}

package com.itheima_02;

import java.util.Scanner;

/*

* 鍵盤錄入兩個數(shù)據(jù),獲取這兩個數(shù)據(jù)的較大值

*

* 分析:

* A:看到鍵盤錄入,我們就應(yīng)該想到鍵盤錄入的三步驟

* 導(dǎo)包,創(chuàng)建對象,接收數(shù)據(jù)

* B:獲取這兩個數(shù)據(jù)的較大值,其實就是判斷兩個數(shù)據(jù)誰大,把大的輸出就可以了。

*

* 導(dǎo)包:

* A:手動導(dǎo)包

* import java.util.Scanner;

* B:鼠標(biāo)點擊紅色叉叉,自動生成

* C:快捷鍵(推薦)

* ctrl+shift+o

*/

public class IfTest {

public static void main(String[] args) {

//創(chuàng)建對象

Scanner sc = new Scanner(System.in);

//接收數(shù)據(jù)

System.out.println("請輸入第一個數(shù)據(jù):");

int a = sc.nextInt();

System.out.println("請輸入第二個數(shù)據(jù):");

int b = sc.nextInt();

//采用if語句格式2實現(xiàn)

/*

if(a>b){

System.out.println("較大的值是:"+a);

}else {

System.out.println("較大的值是:"+b);

}

*/

//拿到較大的值之后,我未必想直接輸出,所以我們定義變量接收這個較大的值

int max;

if(a>b){

max = a;

}else {

max = b;

}

//可能做其他的操作

//max += 100;

System.out.println("較大的值是:"+max);

}

}

package com.itheima_02;

import java.util.Scanner;

/*

* 鍵盤錄入學(xué)生考試成績,請根據(jù)成績判斷該學(xué)生屬于哪個級別

* 90-100 優(yōu)秀

* 80-90 好

* 70-80 良

* 60-70 及格

* 60以下 不及格

*

* 分析:

* A:鍵盤錄入學(xué)生考試成績

* 三步驟

* B:通過簡單的分析,我們決定采用if語句格式3來實現(xiàn)

*

* 程序一定要考慮周全了。

* 安全數(shù)據(jù)

* 邊界數(shù)據(jù)

* 錯誤數(shù)據(jù)

*/

public class IfTest2 {

public static void main(String[] args) {

//創(chuàng)建對象

Scanner sc = new Scanner(System.in);

//接收數(shù)據(jù)

System.out.println("請輸入學(xué)生的考試成績:");

int score = sc.nextInt();

//if語句格式3

/*

if(score>=90 && score<=100){

System.out.println("你的成績屬于優(yōu)秀");

}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("你的成績屬于不及格");

}

*/

//我們發(fā)現(xiàn)程序不夠健壯,加入錯誤數(shù)據(jù)的判斷

if(score<0 || score>100){

System.out.println("你的成績是錯誤的");

}else if(score>=90 && score<=100){

System.out.println("你的成績屬于優(yōu)秀");

}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("你的成績屬于不及格");

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1 順序語句 語句:使用分號分隔的代碼稱作為一個語句。 注意:沒有寫任何代碼只是一個分號的時候,也是一條語句,...
    哈哈哎呦喂閱讀 408評論 0 0
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 1,910評論 0 2
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    葉總韓閱讀 5,165評論 0 41
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,766評論 18 399
  • 查找啟動命令 查看db和用戶 新建用戶 總結(jié) 人在江湖飄,哪能不挨刀。雖然你只想做個靜靜堆代碼不那么美的男(女)紙...
    搬磚的老鮮肉閱讀 1,050評論 0 4