一,运算符
①比较运算符:结果是布尔类型
> < >= <= == !=
②逻辑运算符
&(单与): 与 和 and 逻辑:符号两边均为true最终结果才为true
| (单或): 或 or 逻辑:只要符号两边有一个为true,最终结果才为true
&&(双与):双与 逻辑与单与一致,只是增加了短路的功能
| |(双或): 双或 逻辑与单或一致,只是增加了短路的功能
注意:单路与、或会提高性能,但不是每一种情况都会短路。
③三目运算符
格式:1?2:3
a>b?a:b //如果a>b为true输出a,如果为false则输出b.
④++,--
符号在前,先自增减在使用,符号在后,先使用,再自增减,如下:
public class Demo01 { public static void main(String[] args) { int a = 2; System.out.println(a++);//2 System.out.println(++a);//3 System.out.println(a--);//2 System.out.println(--a);//1 } }
二,多分支判断语句
if(判断条件1){
满足条件 执行的语句;
}else if(判断条件2) {
满足条件 执行的语句;
}else{
以上都不满足 执行语句
}
商品打折案例:
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.println("请输入年的商品金额:");
double price = key.nextDouble();
if (price>=1000&&price<3000){
price = price*0.9;
}else if (price>=3000&&price<5000){
price = price*0.8;
}else if (price>=5000&&price<=10000){
price = price*0.5;
}else if (price>=10000){
System.out.println("你已被打骨折");
}
System.out.println("最终的商品价格为:"+price);
}
}
三,switch case语句
上代码:
public class Demo05 { public static void main(String[] args) { int a = 3; switch (a){ case 1: System.out.println("1");break; case 2: System.out.println("2");break; case 3: System.out.println("3");break; default: System.out.println("0"); } } }
注:1.变量a支持的类型:byte short char int String
2.执行顺序:
会拿着变量a的值一次与每个case后的值做比较,如果不加break会向后穿透所有 case,包括default ,
3.如果设置了保底选项default,并且没有任何case被匹配的话会执行保底选项的内容
4.break和default是可选的,根据自己的业务来决定加不加
四,循环结构
1.for循环
for(初始条件;循环条件;更改条件){循环体}
public class Demo01 { public static void main(String[] args){ for (int i=0;i<10;i++){ System.out.print("* ");//* * * * * * * * * * } } }
用for打印8 88 888 8888
public class Demo02 {
public static void main(String[] args){
for (int i=8;i<=8888;i=i*10+8){
System.out.print(i+" ");
}
}
}
五,debug--断点调试工具
可以帮助我们查看代码执行的顺序
1.什么是断点?
断点是我们认为指定的需要程序执行时停止的地方
2.怎么添加断点并调试?(IDEA版)
双击行数的后面 会出现红点 再选择导航栏的小飞虫 如下 :
3. 可以按F9继续操作或下面的按钮: