学习内容:
- ++和–运算符
赋值运算符
-逻辑运算符 - switch语句
- if else 语句
- for循环 具体内容:
- 一、++和–运算符
- 单独操作的时候,++和–不管放在前面还是后面,结果是一样的 但是,如果参与了运算操作的时候: 如果++或者–在变量后面的时候,先拿变量参与运算操作,后变量进行++或者–; *如果++或者–在变量前面的时候,先变量做++或者–,后拿变量参与运算操作
- 二、赋值运算符
- public class OperatorDemo05 { public static void main(String[] args) { int x = 5; int y = 8; int z = x += y; System.out.println(z); } }
三、逻辑运算符
逻辑&& || & |
- &&具有短路效果。左边是false,右边不执行
- &是无论左边是false还是true,右边都会执行
- ||具有短路效果。左边是false,右边不执行
- |是无论左边是false还是true,右边都会执行
四、switch语句
switch语句 - 表达式的值:byte,short,int,char
- JDK1.5之后,加入了枚举类型
- JDK1.7之后,加入了String类型
- break:中断switch语句的执行
- default:所有的情况都不匹配的时候,就执行该处的语句块
- 掌握switch语句的执行顺序
五、if else 语句
public class IfDemo {
public static void main(String[] args)
{
Scanner sc3 = new Scanner(System.in);
int year = sc3.nextInt();
boolean b1 = year%40;
System.out.println(b1);
boolean b2 = year%100!=0;
boolean b3 = year%4000;
if(b1&&b2||b3){
System.out.println(“闰年”);
}else{
System.out.println(“不是闰年”);
}
sc3.close();
}
}
六、for循环
根据用户输入的年份、月份、 - 判断该年该月有多少天
- 思路:
- 1、使用Scanner用法 三步 :导包、创建对象、接受数据
- 2、使用变量 年份:year 月份:mouth 天:day
*/
public class ForTest {
public static void main(String[] args) {
int sum = 0;
for (int i = 1;i <= 100;i++)
{
if(i % 2 == 0){
sum += i;
}
}
System.out.println(sum);
}
}