一:if 语句格式1(一次判断)
* if(关系表达式){
* 语句体
* }
* 二:if 语句格式2(true执行语句一false执行语句2)(两次判断)
* if(关系表达式){
* 语句体1
* }else{
* 语句体2
* }
* 三:if 语句格式3(多次判断)
* if(){
* 语句1
* } else if(关系表达式2){
* 语句2
* }
* ......
* else{
* 语句n
* }
一、if语句格式1
package com.yupy03;
public class Oeder {
public static void main(String[] args) {
//System.out.println("开始");
//int a = 20; int b = 20;
//if(a==b){
//System.out.println("执行此行语句体");
//}
二、if语句格式2
package com.yupy03;
public class Oeder {
public static void main(String[] args) {
//System.out.println("开始");
//int a = 20; int b = 20;
//if(a==b){
//System.out.println("执行此行语句体");
//}
//=================================================================
//int c = 43;
//if(c%2==0){
//System.out.println("c是偶数");
//}else{
//System.out.println("c是奇数");
//}
//System.out.println("结束");
三、if语句格式3
package com.yupy03;
import java.util.Scanner;//导入键盘录入包
public class Oeder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入温度:");
int qiWen = sc.nextInt();//获取键盘录入数据
if(qiWen>=30 && qiWen<=40){
System.out.println("温度在30~40度适合穿短袖或背心");
}else if(qiWen>=20 && qiWen<=29){
System.out.println("温度在20~29度适合穿卫衣和外套");
}else if(qiWen>=10 && qiWen<=28){
System.out.println("温度在10~28度适合穿毛衣或者外套");
}else if(qiWen>=-20 && qiWen<=9){
System.out.println("温度在-20~9度适合穿袄子厚外套");
}else
System.out.println("您输入的数值不正确!!!!!");
}
}
例:输入英语成绩显示阶段结果 如下:
package com.yupy03;
import java.util.Scanner;
public class Oeder02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入英语成绩分数:");
int score = sc.nextInt();
if(score>100 && score<0){
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("不及格");
}
//=============================================================================
// System.out.println("输入第二个值:");
// int b = sc.nextInt();
// if (a>b){
// System.out.println("a>b:"+a);
// }else if(a<b){
// System.out.println("a<b:"+b);
// }else{
// System.out.println("a=b");
// }
}
}
学习,永无止境,加油!