只有变量能使用该运算符
IDEA的调试
打断点之后按Debug,变成蓝色底色:程序还未执行当前行
1.逐过程 2.逐语句 3.强制步入 4.步出 5.运行到光标处
public class wew {
public static void main(String[] args) {
int a = 1;
a = a++;
System.out.println(a);
}
}
a的值没改变,因为a++先使用a的值再自增
关系运算符( >,<,>=,<=,!=,==)
不能写(3<a<<5)
sout出来是ture或false
例如:int a = 10;int b = 20;sout(a>b);输出结果是false
逻辑运算符(表达式均为布尔表达式)
返回的数据要么是ture要么是false
1.逻辑与&&
表达式1 && 表达式2
短路与&&
若表达式1是false则不会执行表达式2
2.逻辑或II
表达式1 || 表达式2
短路或||
若表达式1为ture,则不执行表达式2、
3.逻辑非! 只能结合布尔表达式使用
位运算符:每一个位运算符都要记住其特点
(用法和逻辑与/或一样,但不支持短路)
1.按位与&:对应位上都是1结果为1
0000 1011
0000 1100 &
------------------------
0000 1000
2.按位或|:对应位上有1结果为1
0000 1011
0000 1100 |
---------------------------
0000 1111
3.按位异或^:对应位上数一样为0,不一样为1
0000 1011
0000 1100 ^
----------------------------
0000 0111
4.按位取反 ~:0111~1000
移位运算
移动负数位或移动太大都没意义
0000 1011(11)
1.左移<< 右边补0
0000 1011 << 1 = 0001 0110
0000 1011 << 2 = 0010 1100
2.右移>> 左边补符号位
0000 1011 >> 1 = 0000 0101
0000 1011 >> 2 = 0000 0010
3.无符号右移
1111 1111(-1)
1111 1111 >>> 1 = 0111 1111
条件表达式
表达式1?表达式2:表达式3;
运算符的优先级:+的优先级高于>>
逻辑控制:(顺序、选择、循环结构)
输入数据:
Scanner scan = new Scanner(System.in)
前提:import java.until.Scanner;这个就写Scan按回车就自动出来了
然后写int a = scan.nextInt();
分支语句
1.if(布尔表达式){ }
例:判断year是否为闰年
(世纪闰年:是100倍数且是400倍数,普通闰年:是4的倍数且不是100的倍数)
import java.util.Scanner;
public class wew {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
if(a % 100 == 0){
if (a % 400 == 0){
System.out.println("是世纪闰年");
}
}
else{
if (a % 4 == 0){
System.out.println("是普通闰年");
}
else{
System.out.println("不是闰年");
}
}
}
}
2.switch(表达式){
case:
break;
default:
break;
}
switch参数的数据类型有哪些?
基本类型:byte、char、short、int
引用类型(再case后面加双引号):String常量串、枚举类型
switch不能表达复杂的条件,例如有&&运算符时
循环结构
1.while(布尔表达式){
语句块; }
例:打印1-10的数字
public class wew {
public static void main(String[] args) {
int a = 1;
while(a <= 10){
System.out.println(a);
a++;
}
}
}
例:计算1-100的和
public class wew {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i <= 100){
sum += i;
i++;
}
System.out.println(sum);
}
}
例:计算5!
public class wew {
public static void main(String[] args) {
int i = 1;
int ret = 1;
while(i <= 5){
ret *= i;
i++;
}
System.out.println(ret);
}
}
例:计算1!+2!+3!+4!+5!
public class wew {
public static void main(String[] args) {
int sum = 0;
int k = 1;
while(k <= 5){
int ret = 1;
int i = 1;
while(i <= k){
ret *= i;
i++;
}
sum += ret;
k++;
}
System.out.println(sum);
}
}
1.break结束整个循环 2.continue结束本趟循环
public class wew {
public static void main(String[] args) {
int i = 1;
while (i <= 10){
if(i == 3){
continue;
}
System.out.println(i);
i++;
}
}
}
continue下方代码不会被执行,要想输出1245678910,就在continue前面写i++;
例:求1-100间所有能被3和6整除的整数(必须用break或continue)
public class wew {
public static void main(String[] args) {
int i = 1;
while (i <= 100){
if (i % 6 != 0){
i++;
continue;
}
System.out.println(i);
i++;
}
}
}
for循环
for(表达式1;布尔表达式2;表达式3){
表达式4; }
执行顺序:1243
java快速写for循环方法:fori回车
例:计算5!
public class wew {
public static void main(String[] args) {
int ret = 1;
for (int i = 1; i <= 5; i++) {
ret *= i;
}
System.out.println(ret);
}
}
例:计算1!+2!+3!+4!+5!
public class wew {
public static void main(String[] args) {
int sum = 0;
for (int k = 1; k <=5 ; k++) {
int ret = 1;
for (int i = 1; i <= k; i++) {
ret *= i;
}
sum += ret;
}
System.out.println(sum);
}
}
读入操作
import java.util.Scanner;
public class wew {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("读入姓名");
//String name = scan.next();其中next遇到空格后,读取结束
// System.out.println(name);
//若读取中有空格,建议使用next.Line
String name = scan.nextLine();
System.out.println(name);
System.out.println("读入年龄");
int age = scan.nextInt();
System.out.println(age);
//若读入类型不匹配,会报错Input Mismatch Exception
System.out.println("读入工资");
float money = scan.nextFloat();
System.out.println(money);
scan.close();//用完了,就释放
}
}
先读int再读string时,string会把回车当做字符,如果真有先int再string的读入情况,就在读入string之前再加一个scan.nextLine();就把那个空格先吸收读入了 。
(比特听课笔记)