二、运算符和表达式
1、算数运算符
(1)四则运算:包括+、-、*、/
- 整数除法得到整数,此时可以使用取余%
- 不同数据类型运算,结果为数据范围大的
15 / 3 = 5
15 % 2 = 1
15.0 / 3 = 5.0
- 整数/0 返回异常,浮点数/0 返回Infinity (inf)
System.out.println(15/0);//Exception in thread "main" java.lang.ArithmeticException: / by zero
System.out.println(15.0/0);//Infinity
(2)加号的多种用法:
- 数值型,就是加法;
- char型,先提升为int进行计算;
- string字符串型,字符串连接操作,字符串与任何数据类型进行连接,都会变为字符串 “”表示字符串
public class Demo05Plus {
public static void main(String[] args){
String str1 = "Hello";
System.out.println(str1+"world"); //Helloworld
System.out.println(str1);//Hello
System.out.println(str1+20);//Hello20
System.out.println(str1+(20+30));//Hello50
}
}
2、自增自减运算符:++ 与 --
可以a++,也可以++a,但存在区别
- 单独使用
a++与++a 一样 - 混合使用
a++ 先用后加,++a 先加后用,自减也一样
int x=10;
int y=20;
int result3=++x + y--;//x先加后用,y先用后减,11+20=31
System.out.println(result3);
System.out.println(x);//11
System.out.println(y);//19
注意:常量不能用自增自减运算符,如4++是错误的
3、赋值运算符:
- 基本赋值:=
int a=30;
- 复合赋值运算:+= -= *= /= %=
a+=1; // 相当于a=a+1
注意:常量不能赋值,比如30=50,同时有时会强制转换类型
byte num=30;
num+=5; //相当于byte+int = int
4、比较运算符:返回true false
== ; <= ; != ; >= ; > ; <
5、逻辑运算符:
- 与(并且) &&
- 或(或者) ||
- 非(取返) !
逻辑运算具有短路效果,根据左边已给条件先判断,后面不会执行
int a=10;
System.out.println(3>4 && ++a<10);
System.out.println(a);//10,++a没有执行
对于1<x<3,写成1<x && x<3,与python不同
6、三元运算符:
一元运算符:取反!,++ ,--
二元:加法+ , 赋值=
三元运算符:三个数据
格式:
/* 数据类型 变量名称 = 条件判断 ? 表达式A:表达式B;*/
int a=10;
int b=20;
int max = a > b ? a:b;//max=20
注意:必须同时保证表达式A和表达式B都符合左侧数据类型的要求,且三元运算符的结果必须被使用。可以被打印。但不能直接 :
a > b ? a:b
7、位运算符:对于整型数值
&("and")、|("or")、^("xor")、~("not") >>右移 <<左移
System.out.println(1<<1);//1:0001 2 0010
System.out.println(1>>1);//1:0001 0 0000
8、运算优先级
9、Math函数:java.lang.Math类
double x =10;
double y =Math.sqrt(x);
double z =Math.abs(x);
还包括三角函数、指数、对数等,以及Math.PI、Math.E
三、控制流程
1、条件语句
(1) if
if (条件表达式){
选择语句;
}
不符合条件 直接跳过
public class Demo02If {
public static void main(String[] args){
System.out.println("发现网吧");
int age=16;
if (age>=18){
System.out.println("进入网吧!");
System.out.println("打游戏!");
}
System.out.println("回家吃饭");
}
}
(2) if else
if (条件表达式){
选择语句1;
} else {
选择语句2;
}
以下是例子
if (num % 2==0){
System.out.println(num+"是偶数");
} else {
System.out.println(num+"是奇数");
}
(3)if .. else if ..else
if (条件表达式){
选择语句1;
} else if (条件表达式){
选择语句2;
} else {
选择语句n;
}
例子,与三元运算符的比较
public class Demo06Max {
public static void main(String[] args){
int a =10;
int b=20;
int max;
if (a>b){
max=a;
} else{
max=b;
}
System.out.println(max);
int max1 = a>b? a:b;
System.out.println(max1);
}
}
2、循环语句
(1) for
5.fori \\快速输入循环
for (int i =1;i<=100;i++){
System.out.println("我错了"+i);
}
System.out.println("程序停止");
注意:该案例变量i的作用域就为for循环的整个循环体,不能在循环体外使用,因此需要在外部定义。
int i;
for ( i =1;i<=100;i++){
System.out.println("我错了"+i);
}
System.out.println("程序停止");
可以在不同循环内定义相同变量,比如
for (int i =1;i<=100;i++){
System.out.println("我错了"+i);
}
for (int i =110;i<=200;i++){
System.out.println("我又错了"+i);
}
(2) while
int i=1;//初始化语句;
while (i<=100){
System.out.println("我错了"+i);
i++;//步进语句
}
System.out.println("程序停止");
等价于上面的第一个for循环
(3) do while
int i=1;//初始化语句;
do {
System.out.println("我错了"+i);
i++;
}while (i<=100);
System.out.println("程序停止");
for、while 有时会执行0次,do-while至少执行一次
(4) 死循环
while(true){}
或者
for(int i=1;i<=10;){
if(i==4){
continue;//跳过当前次循环,马上开始下一次循环
}
System.out.println(i+"到了");
}
(5) 循环嵌套
for(int hour=0;hour<24;hour++){
for(int minute=0;minute<60;minute++){
System.out.println(hour+"时"+minute+"分");
}
}
3、多重选择语句:switch
switch(num){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("数据不对");
break;
}
注意:
- 多个case后面的数不能重复
- switch后面小括号数据类型只能是:
基本数据类型: byte/short/char/int
引用数据类型: String字符串、enum枚举 - switch 前后顺序可以颠倒,break可以省略。
- default可以放在case前面,因此default不一定最后一个
- 没有break会继续向下执行
case 1:
System.out.print1n("星期一");
//break;
case 2:
System.out.printin("星期二");
break;
\*没有break,穿透这个case1,输出为
星期一
星期二
*\
4、中断与跳过:break与continue
(1) break的用法
for(int i=1;i<=100;i++){
if(i==4){
break;
}
System.out.println("hello"+i);
}
break语句用于退出循环
(2) continue的用法
for(int i=1;i<=10;i++){
if(i==4){
continue;//跳过当前次循环,马上开始下一次循环
}
System.out.println(i+"到了");
}
break语句用于跳过当前次循环,马上开始下一次循环,案例中没有输出“4到了”
5、块作用域
块确定了变量的作用域,在作用域外部,该变量不可使用
public static void main(String[] args)
{
int n;
...
{
int k;
//int n;//无法通过编译,不能在嵌套的两个块中声明同名的变量
...
}
}