java中的if判断_18.java中判断(if…else)

本文详细介绍了编程中的顺序语句、判断语句(if...else)及其不同格式,包括if条件判断、if...else嵌套判断以及if...elseif...else多条件判断。通过实例展示了如何根据用户输入判断数字的属性,如是否为5或2的倍数,奇偶性等。同时,讨论了if语句的常见错误,如缺少括号和在if后加分号的问题。最后,给出了判断闰年的逻辑表达式,帮助读者理解条件判断的实际应用。
摘要由CSDN通过智能技术生成

顺序语句

语句:使用分号分隔的代码称作为一个语句。

25bb609dcbc5bb6db5d4517f36fdaf16.png

注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句。

7d79b06f760e898f8ecb7e9f1a30d562.png

顺序语句就是按照从上往下的顺序执行的语句。

判断(if…else)

在我们找工作的过程中,要求两年工作经验以上且年龄超过30岁。

什么是判断语句:用于判断的语句叫判断语句。

1.格式一

if(判断条件){

如果符合条件执行的代码;

执行的代码块1;

执行的代码块2;

……………….;

执行的代码块n;

}

338b83e816dc3ec9b9a77fe811c3e766.png

练习:提示用户输入一个整数。如果该整数是5的倍数,打印“5的倍数”如果是2的倍数打印“2的倍数”

提示:为了便于让用户输入数据,我们使用Scanner这个类,固定用法Scanner sc=new Scanner(System.in); 该类需要导入包import java.util.Scanner;

int nextInt = sc.nextInt();获取用户输入的数字

importjava.util.Scanner;public classDemo9 {public static voidmain(String[] args) {

Scanner sc=newScanner(System.in);int nextInt =sc.nextInt();if(nextInt%5==0){

System.out.println("是5的倍数");

}if(nextInt%2==0){

System.out.println("是2的倍数");

}

}

}

2.格式二

if(判断条件){

执行的代码块1;

执行的代码块2;

……………….;

执行的代码块n;

}else{

执行的代码块1;

执行的代码块2;

……………….;

执行的代码块n;

}

ecd0550fb5ac97cfa9ebe3d9beae3b06.png

案例:判断一个整数是奇数还是偶数

public static voidmain(String[] args) {

Scanner sc= newScanner(System.in);

System.out.println("请输入一个整数:");int nextInt =sc.nextInt();if (nextInt % 2 == 0) {

System.out.println("是偶数");

}else{

System.out.println("是奇数");

}

System.out.println("over");

}

同样道理如果花括号中只有一条语句,那么花括号可以省略不写,初学者不推荐省略。

public static voidmain(String[] args) {

Scanner sc= newScanner(System.in);

System.out.println("请输入一个整数:");int nextInt =sc.nextInt();if (nextInt % 2 == 0)

System.out.println("是偶数");elseSystem.out.println("是奇数");

System.out.println("over");

}

观察发现if else语句有点类似于三元运算符.其实三元运算符是if else 的一种简写格式.

Public static voidmain(String[] args) {int x = 0, y = 1, b;//if else 语句

if (x >y) {

b=x;

}else{

b=y;

}

System.out.println(b);//1//3元运算

b = x > y ?x : y;

System.out.println(b);//1

}

这两种格式是一样的。if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;

三元运算符:

好处:可以简化if else代码。

弊端:因为是一个运算符,所以运算完必须要有一个结果。

3.格式三

if(判断条件1){

执行的代码块1;

}else if(判断条件2){

执行语句;

}else if(判断条件3){

执行语句;

}

需求: 根据用户定义的数值不同,打印对应的星期英文。if 只能进行一层判断,if else 只能进行两层判断,那么需要多层判断时呢?星期可是有7个数的。如何设计代码?

使用if 语句

public static voidmain(String[] args) {int x = 8;if (x == 1) {

System.out.println("星期一");

}if (x == 2) {

System.out.println("星期二");

}if (x == 3) {

System.out.println("星期三");

}

}

如果这样设计的话,第一个if语句执行完毕后,第二个语句仍会执行(去判断),是一个顺序结构.那么事实上当前定义的星期之后会有一个.假如,第一个已经符合条件,那么剩余的执行就没有意义了。属于逻辑错误。

使用if else ,如果用户输入的是7以外的数据,那么怎么处理?就需要使用else 了

方案2:使用if else if语句

public static voidmain(String[] args) {int x = 8;if (x == 1) {

System.out.println("星期一");

}else if (x == 2) {

System.out.println("星期二");

}else if (x == 3) {

System.out.println("星期三");

}else if (x == 4) {

System.out.println("星期四");

}else if (x == 5) {

System.out.println("星期五");

}else if (x == 6) {

System.out.println("星期六");

}else if (x == 7) {

System.out.println("星期日");

}else{

System.out.println("请输入数字1-7");

}

}

注意:

Public static voidmain(String[] args) {int x = 5;if (x == 1) {

System.out.println("1");

}if (x == 2) {

System.out.println("2");

}if (x == 3) {

System.out.println("3");

}else{

System.out.println("4"); //4

}

}

该if 语句不是一个整体,第一个if 是一个语句,第二个又是一个语句,最后的if else 又是一个语句。

if语句特点

第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。

条件表达式无论写成什么样子,只看最终的结构是否是true 或者 false。

练习1: 根据用户输入的月份,打印出月份所属的季节.

练习2: 根据用户输入的成绩,进行评级,根据学生考试成绩划分ABCD

练习1:

public static voidmain(String[] args) {int x = 1;if (x == 3) {

System.out.println("spring");

}else if (x == 4) {

System.out.println("spring");

}

}

仔细观察:发现if和else if要执行的语句是一样的,可不可以合并呢。当然是可以的。怎么合并?使用逻辑运算符,那么使用哪个逻辑运算符呢, &肯定不行。需要全部为真才为真,月份是不可能同时满足的 那么使用|连接符号即可。意思只要其中一个为真,就为真。另外可以使用短路功能。

public static voidmain(String[] args) {int x = 1;if (x == 3 || x == 4 || x == 5) {

System.out.println("spring");

}else if (x == 6 || x == 7 || x == 8) {

System.out.println("Summer");

}else if (x == 9 || x == 10 || x == 11) {

System.out.println("autumn");

}else{

System.out.println("Winter");

}else{

System.out.println("月份不存在");

}

}

练习2:

public static voidmain(String[] args) {

Scanner sc= newScanner(System.in);

System.out.println("请输入考试分数:");double score =sc.nextDouble();chargrade;if (score >= 90.0)

grade= 'A';else if (score >= 80.0)

grade= 'B';else if (score >= 70.0)

grade= 'C';else if (score >= 60.0)

grade= 'D';elsegrade= 'F';

System.out.println("你的成绩是:" +grade);

}

If语句常见的错误:

1.忘记必要的括号:如果代码块中只有一条语句的时候,可以省略花括号,但是当花括号将多条语句扩在一起时,花括号就不能在省略。

double radius = 4;doublearea;if (radius >= 0)

area= radius * radius * 3.14;

System.out.println("The area " + " is " + area);

double radius = 4;doublearea;if (radius >= 0) {

area= radius * radius * 3.14;

System.out.println("The area " + " is " +area);

}

虽然代码一样多,但是第一个会编译报错(area没有出初始化),第二个正常运行。就是因为少了花括号。所以一定要仔细.

2.if语句后出现分号

double radius = 0;doublearea;if (radius > 0); {

area= radius * radius * 3.14;

System.out.println("The area " + " is " +area);

}

注意:这是一个逻辑错误,编译和运行都不会报错,只是不会出现想要的结果。

相当于判断符合条件后,执行一个空语句

double radius = 0;doublearea;if (radius > 0){}{

area= radius * radius * 3.14;

System.out.println("The area " + " is " +area);

}

判断闰年

1:什么是闰年?可以被4整除不能被100整除,或者可以被400整除,那么这一年就是闰年(leap year)

public static voidmain(String[] args) {

Scanner sc= newScanner(System.in);

System.out.println("请输入年份:");int year =sc.nextInt();//判断年份能否被4整除

boolean isLeapYear = (year % 4 == 0);//年份能被4整除,并且不能被100整除并且使用&&(and)

isLeapYear = isLeapYear && (year % 100 != 0);//年份或者能够被400整除

isLeapYear = isLeapYear || (year % 400 == 0);if(isLeapYear) {

System.out.println(year+ "是闰年!");

}//简写格式;

if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {

System.out.println(year+ "是闰年!");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值