Java基础-day02

mac2024-08-22  60

自增和短路与

public class Test01 { public static void main(String[] args) { int i1 = 5; boolean result = (i1++ > 5) && (++i1 > 4); System.out.println(result); //false System.out.println(i1); //6 } }

i1++>5先进行i1>5的判断,再进行自增至6。5不大于5,判断为false。短路与不再执行后段代码,但是前段代码已执行完毕,i1此时值为6。

自增和短路或

public class Test02 { public static void main(String[] args) { int i1 = 5; boolean result = (i1++ > 5) || (++i1 > 4); //i1=7 System.out.println(result); //true System.out.println(i1); //7 } }

i1++>5先进行i1>5的判断,再进行自增为6。5不大于5,判断为false。短路或继续执行后半段代码++i1,i1先自增为7再进行i1>4的判断,判断结果为true,i1此时值为7。

分支判断语句练习题

输入月份,判断对应季节

/* 分析以下需求并实现: 1.功能描述:键盘录入月份,输出对应的季节 2.要求: (1)定义一个月份,值通过键盘录入; (2)输出该月份对应的季节 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12,1,2 冬季 (3)演示格式如下: 定义的月份:5 控制台输出:5月份是春季 */ import java.util.*; public class Season { public static void main(String[] args) { int month; Scanner sc = new Scanner(System.in); System.out.println("请输入月份:"); month = sc.nextInt(); if (month >= 3 && month <= 5) { System.out.println(month + "月份是春季"); } else if (month >= 6 && month <= 8) { System.out.println(month + "月份是夏季"); } else if (month >= 9 && month <= 11) { System.out.println(month + "月份是秋季"); } else if (month == 12 || month == 1 || month == 2) { System.out.println(month + "月份是冬季"); } else { System.out.println("输入有误,请输入正确的月份"); } } }
最新回复(0)