2020-7-19-day2
运算符
进行特定操作的符号
算术运算符
+,-,*,/,取模 %
自增运算符:++ , 自减运算符 - -
前++ 先+后使用 ---------------- 后++ 先使用后+
注意事项
- 运算中有不同的数据类型,结果是数据类型大那种
- 对于字符串(String),“+”代表字符串连接 ,任何数据类型和字符串连接都会变成字符串。
赋值运算符
基本赋值运算符:= 右边赋值给左边
复合赋值运算符:+= ,-= ,*= , /= , %=
比较运算符
== (等于), < , > , <= , >= , != (不等于)
注意事项
- 比较运算符结果是一个Boolean值,成立true,不成立false
- 如果进行多次判断,不能连着写
逻辑运算符
与 && ,或 || , 非 !
注意事项
- 与“&&”和或“||”,具有短路效应,只要由左边可以得到最终结果,右边就不再执行判断了,从而节省一定的性能。
- 逻辑运算符只能用与Boolean值,与或需要两个,取反只需要一个。
- 两个条件:条件A && 条件B
多个条件:条件A && 条件B && 条件C
三元运算符
格式 : 数据类型 变量名称 = 条件判断 ? 表达式A : 表达式B;
注意事项
- 表达式A和表达式B都符合左边变量类型
- 三元运算符结果必须被使用:打印或者赋值给变量
代码示例
public class Day2 {
public static void main(String[] args) {
//运算符
//算数运算符
System.out.println("加法:"+(2+3)+",减法:"+(3-2)+",乘法:"+(2*3)+",除法:"+(3/2)+",取模:"+(3%2));
int x=10,y=20;
System.out.println(x++);//10
System.out.println(++y);//21
//赋值运算符
int a=30,b=40,c=50,d=60;
System.out.println(a+=10);//a=a+10=40
System.out.println(b/=10);//b=b/10=4
System.out.println(c-=10);//c=c-10=40
System.out.println(d*=10);//d=d*10=600
//比较运算符
int e=3;
System.out.println(3>4);//false
System.out.println(e==3);//true
System.out.println(e!=3);//false
//逻辑运算符
int f=10,g=20,h=30;
System.out.println(f==10 || g!=20);//true
System.out.println(g==10 && ++h==31);//false
System.out.println(h);//30
//三元运算符
int i=10,j=20;
int max;
max=(i>j)?i:j;
System.out.println(max);
}
}
表达式
运算符连接起来的式子
流程
选择结构
if语句
格式:
if(关系表达式){语句体};
if…else语句
格式:
if(关系表达式){语句体1} else{语句体2};
if…else if…else语句
格式:
if(关系表达式){语句体1} else if(关系表达式2){语句体2} else{语句体3};
switch语句
格式:
switch(){ case 常量值1:语句体1;break; case常量值2:语句体2;break; default : 语句体n;break;}
注意事项
-
case后面的值不能重复
-
switch后加数据类型:
基本数据类型:byte/short/char/int
引用数据类型:String字符串,enum枚举 -
switch语句顺序可以随机
public class Day2 {
public static void main(String[] args) {
//选择结构
//if语句
{
int age=19;
if(age>=18) {
System.out.println("进入网吧");
}//进入网吧
}
//if else语句
{
int age=16;
if(age>=18) {
System.out.println("进入网吧");
}else {
System.out.println("回家吃饭");
}//回家吃饭
}
//if else if else 语句
{
int x=5,y;
if(x>3 && x<10) {
y=2*x;
}else if(x>10) {
y=3*x;
}else {
y=x;
}
System.out.println(y);
}
//switch语句
{
int x=1;
switch(x) {
case 0:
System.out.println(0);
break;
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2);
break;
default:
System.out.println("No ");
break;
}
}
}
}
循环结构
for语句
格式:
for(初始化表达式;布尔表达式;步进表达式){循环体}
while循环
格式:
while(条件判断){循环体}
do while循环
格式:
do{循环体}while(条件判断);
三种循环区别
- do while会至少运行一次
- for循环变量定义在括号内,只有在小括号内可以使用
public class Day2 {
public static void main(String[] args) {
//for循环
{
for(int i=0;i<100;i++) {
System.out.println(i);
}
}
//while循环
{
int i=0;
while(i<100) {
System.out.println(i);
i++;
}
}
//do while循环
{
int i=0;
do {
System.out.println(i);
i++;
}while(i<100);
}
}
}
条件控制关键字
break
在switch中使用,一旦执行,整个语句立即结束
在循环语句中使用,整个循环语句结束 ,跳出循环
continue
立刻跳过当前次的循环,执行下次循环
public class Day2 {
public static void main(String[] args) {
for(int i=1;i<=10;i++) {
if(i==4) {
break;//打断循环
}
System.out.println(i);
//1 2 3
}
//continue
for(int i=1;i<=10;i++) {
if(i==4) {
continue;//跳过循环
}
System.out.println(i);
//1 2 3 5 6 7 8 9
}
}
}
死循环
格式:
while(true){}
方法
若干语句的功能集合
基本格式:
public static void xxx() {};
调用格式:xxx();
具体格式:修饰符 返回值类型 方法名称(参数类型 参数名称,....){方法体 return 返回值;}
修饰符:现阶段固定写法:public static
返回值类型:最终结果的数据类型
方法名称:方法名字,小驼峰写法
参数类型:传入参数类型; 参数名称:传入参数名字
- 方法定义无先后顺序,不可以嵌套
- 方法定义后,要使用需要调用
- 方法定义在类中
- 如果方法有返回值,需要写return
- return返回数据必须和返回值类型一样
- return可以省略 void
- 一个方法可以有多个return 但是不可以同时实现
方法的三种调用
- 单独调用:方法名称(参数);
- 赋值调用:数据类型 数据名称=方法名称(参数);
- 打印调用:system.out.println(方法名称(参数));
public class Day2 {
public static void main(String[] args) {
int x=sum(2,3);
System.out.println(x);
}
public static int sum(int a,int b) {
int result = a+b;
return result;
}
}
方法重载(overload)
多个方法的名称相同,但是参数列表不同
注意事项
- 参数个数不同,类型不同,参数的类型顺序不同 可以重载
- 与 参数名称 , 方法返回值类型无关