【Java基础】操作符&&流程控制
- 算数运算符
- 关系运算符
- 逻辑运算符
- 位运算符
- 赋值运算符
- 三元运算符
和c++的内容一致
长路与短路逻辑运算符
||(短) |(长) && (短)&(长) 短路可以节省运算时间。
int i = 1; boolean b = !(i++ == 3) ^ (i++ ==2) && (i++==3); System.out.println(b); System.out.println(i);
&&是短路运算符,前面判定为false时直接不会执行后面。
无符号右移>>>
如果是一个负数,那么对应的二进制的第一位是1
无符号右移>>>会把第一位的1也向右移动,导致移动后,第一位变成0
这样就会使得负数在无符号右移后,得到一个正数
以二进制补码形式存储
- Scanner
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); int a = s.nextInt(); float b = s.nextFloat(); String c = s.next,Line(); //回车换行 } }
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int i; Scanner s = new Scanner(System.in); i = s.nextInt(); System.out.println(i); } }
流程控制
- if条件判断
- Switch
switch(day){ case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; default: System.out.println("这个是什么鬼?"); }
- while / do-while
- for
- continue
- break
使用标签结束外部循环
public class HelloWorld { public static void main(String[] args) { //打印单数 outloop: //outloop这个标示是可以自定义的比如outloop1,ol2,out5 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.println(i+":"+j); if(0==j%2) break outloop; //如果是双数,结束外部循环 } } } }
浙公网安备 33010602011771号