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