运算符与分支流程控制
运算符
运算符的概念
执行特定的数学或逻辑等操作的符号
算数运算符
四则运算是最常见的数学运算,运算规则与数学中的数学运算基本一样,也有一些细微的区别
整数除法得到整数结果(无论是否可以被整除)!
public class ArithmeticDemo { public static void main(String[] args) { int a = 50; int b = 100; System.out.println(a+b);
a = Integer.MAX_VALUE; b =1; System.out.println(a+b);
long l = (long) a + b ; System.out.println(l);
a = 9; b =2 ; System.out.println(a /2 );
double d = (double) a / b; System.out.println(d); byte b1 = 5; short s1 =6; short s2 = (short) (b1 + s1); byte b2 = 3 + 100; byte b3 = (byte) (100 + 100); } }
|
public class IncrementDemo { public static void main(String[] args) { int i = 1 ; System.out.println(i++); System.out.println(i);
i = 1; System.out.println(++i); System.out.println(i);
i =1; System.out.println(i--); System.out.println(i);
i =1 ; System.out.println(--i); System.out.println(i); } }
|
关系运算符
关系运算符用于比较数值的大、小、相等关系
关系运算结果是 boolean 类型数据
关系成立则为true 不成立则为false
== != > >= < <=
public class RelationalDemo { public static void main(String[] args) { int age = 5; System.out.println(age > 12); System.out.println(age >= 12); System.out.println(age < 12); System.out.println(age <= 12); System.out.println(age == 5); System.out.println(age != 5); } }
|
逻辑运算符
参与逻辑运算的变量或表达式都是boolean类型,运算结果也是boolean类型
运算符有三种 逻辑与&& 逻辑或|| 逻辑非 !
[案列] 闰年判断
public class LogicalDemo1 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入年份:"); int year = console.nextInt(); boolean b = (year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0); System.out.println(b); } }
|
短路逻辑运算
在进行两元逻辑运算时,如果能够通过第一个表达式得到整体运算结果,就不再运算第二个表达式
java的两元逻辑运算符 && 和 || 都按照短路规则执行.
短路运算规格的优点
- 可以减少运算次数提高程序性能
- 利用第一个表达式运算结果跳过第二个表达式,避免第二个表达式的运算错误
public class LogicalDemo2 { public static void main(String[] args) { int i = 10; boolean b = i >100 && i++>100; System.out.println(b); System.out.println(i);
boolean b2 = i <100 && i++ <100; System.out.println(b2); System.out.println(i);
boolean b3 = i< 100 || i++ <100; System.out.println(b3); System.out.println(i);
boolean b4 = i >100 || i++ <100; System.out.println(b4); System.out.println(i);
} }
|
赋值运算符
基本的赋值运算符是”=”
public class AssignmentDemo2 { public static void main(String[] args) { int a,b,c,d; a=b=c=d=20; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d);
} }
|
复合运算符
复合赋值运算可以自动执行强制类型转换
public class AssignmentDemo3 { public static void main(String[] args) { int i = 1; i +=5; System.out.println(i);
i = 10; i -= 5; System.out.println(i);
i += 5.5; System.out.println(i); } }
|
字符串连接运算符
字符串连接运算就是将两个字符串连接为一个新的字符串
java字符串连接使用”+”实现
public class StringDemo { public static void main(String[] args) { System.out.println(5 + 5.5); System.out.println("5"+5.5); int i = 10; System.out.println("i加10的和是:"+ (i + 10)); } }
|
三元运算符
三元/目运算符也称为”条件运算符”或者”条件表达式”
boolean表达式 ? 表达式1 : 表达式2
import java.util.Scanner;
public class TernaryDemo { public static void main(String[] args) {
Scanner console = new Scanner(System.in); System.out.println("请输入两个数字"); int a = console.nextInt(); int b = console.nextInt(); int max = a > b ? a : b; System.out.println(max);
} }
|
分支流程控制
if语句
if单路分支流程控制
import java.util.Scanner;
public class IfDemo01 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入商品总价:"); double total = console.nextDouble(); System.out.println("原价是"+total); if (total >= 500){ total *= 0.8; } System.out.println("实付款:"+total); } }
|
if else 双路分支语句
import java.util.Scanner;
public class IfDemo01 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入商品总价:"); double total = console.nextDouble(); System.out.println("原价是"+total); if (total >= 500){ total *= 0.8; } System.out.println("实付款:"+total); } }
|
if else if 多路分支语句
import java.util.Scanner;
public class IfDemo3 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("输入总金额"); double total = console.nextDouble(); if (total > 0) { System.out.println("总价是"+total); if (total >= 800) { total *= 0.7; } else if (total >= 500) { total *= 0.8; } else if (total >= 200) { total *= 0.9; } else { total *= 0.95; } System.out.println("实付金额:"+total); } else { System.out.println("输入有误"); } }
}
|
switch语句
switch case 是java的一种多路分支语句,可以根据整数、字符串、枚举类进行分支
switch case语句
整数判断建议使用Switch 效率高
程序从满足条件处切入进来,到执行到break结束
import java.util.Scanner;
public class SwitchDemo01 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("输入分数:"); int score = console.nextInt(); switch (score/10){ case 10: case 9: System.out.println("A"); break; case 8: System.out.println("B"); break; case 7: case 6: System.out.println("C"); break; default: System.out.println("D"); } } }
|
java新特性-Switch表达式
import java.util.Scanner;
public class SwitchDemo02 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("请输入分数"); int score = console.nextInt(); switch (score/10){ case 10,9 -> System.out.println("A"); case 8 -> System.out.println("B"); case 7,6 -> System.out.println("C"); default -> System.out.println("D"); } } }
|