package com.itheima_02;
/*
* if語句有三種格式宴树。
*
* if語句格式1:
* if(關系表達式) {
* 語句體;
* }
*
* 執(zhí)行流程:
* A:首先判斷關系表達式看其結果是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("結束");
}
}
ackage com.itheima_02;
/*
* if語句格式2:
* if(關系表達式) {
* 語句體1;
* }else {
* 語句體2;
* }
*
* 執(zhí)行流程:
* A:判斷關系表達式的值是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("結束");
}
}
ackage com.itheima_02;
/*
* if語句格式3:
* if(關系表達式1) {
* 語句體1;
* }else if(關系表達式2) {
* 語句體2;
* }else if(關系表達式3) {
* 語句體3;
* }
* ...
* else {
* 語句體n+1;
* }
*
* 執(zhí)行流程:
* A:首先判斷關系表達式1看其結果是true還是false
* B:如果是true蠢莺,就執(zhí)行語句體1
* ? ? 如果是false零如,就繼續(xù)進行關系表達式2的判斷看其結果是true還是false
* C:如果是true,就執(zhí)行語句體2
* ? 如果是false考蕾,就繼續(xù)進行關系表達式...的判斷看其結果是true還是false
* ...
* D:如果沒有一個為true的,就執(zhí)行語句體n+1
*
* if語句的三種格式:
* 第一種格式適合做一種情況的判斷
* 第二種格式適合做二種情況的判斷
* 第三種格式適合做多種情況的判斷
*/
public class IfDemo3 {
public static void main(String[] args) {
// x和y的關系滿足如下:
// 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:看到鍵盤錄入矛紫,我們就應該想到鍵盤錄入的三步驟
* 導包牌里,創(chuàng)建對象务甥,接收數(shù)據(jù)
* B:獲取這兩個數(shù)據(jù)的較大值,其實就是判斷兩個數(shù)據(jù)誰大敞临,把大的輸出就可以了。
*
* 導包:
* A:手動導包
* import java.util.Scanner;
* B:鼠標點擊紅色叉叉奏黑,自動生成
* 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;
/*
* 鍵盤錄入學生考試成績蹂匹,請根據(jù)成績判斷該學生屬于哪個級別
* 90-100 優(yōu)秀
* 80-90 好
* 70-80 良
* 60-70 及格
* 60以下 不及格
*
* 分析:
* A:鍵盤錄入學生考試成績
* 三步驟
* 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("請輸入學生的考試成績:");
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("你的成績屬于不及格");
}
}
}