第五天,第六天,开始有输入和输出了
前几天博客园出问题了没法写博客,现在解决好了,现在整理下上上周五和上周三学习的内容
学习视频网址:https://www.bilibili.com/video/BV12J41137hu?p=29
逻辑运算符
1 package operator; 2 3 //逻辑运算符 4 public class demo04 { 5 public static void main(String[] args) { 6 //与(and) 或(or) 非(取反) 7 boolean a = true; 8 boolean b = false; 9 10 System.out.println("a && b: "+(b&&a));//逻辑与运算,只有两个都为真,结果才为true 11 System.out.println("a || b: "+(b||a));//逻辑或运算,两个变量有一个为真,则结果为true 12 System.out.println("!(a && b): "+!(b&&a));//非:如果是真,则变为假,如果是假则变为真 13 14 //短路运算 15 int c = 5; 16 boolean d =(c<4)&&(c++<4);//如果(c<4)为假,则直接返回假,不执行后面的(c++<4) 17 System.out.println(d); 18 System.out.println(c); 19 } 20 }
运行结果

位运算
1 package operator; 2 3 public class demo5 { 4 public static void main(String[] args) { 5 /* 6 A = 0011 1100 7 B = 0000 1101 8 ------------------------------------------------------- 9 A%B = 0000 1100 (都是1才为1,否则为0) 10 A|B = 0011 1101(如果对应为都是0,则为0,否则为1) 11 A^B = 0011 0001 (对应位相同为0,不相同为1) 12 ~B = 1111 0010 (取反) 13 14 例题:2*8怎么算最快? 15 2*8=16 2*2*2*2 16 <<左移*2 17 >>右移/2 18 效率极高 19 20 0000 0010 2 21 0000 0100 4 22 0000 1000 8 23 0001 0000 16 24 */ 25 System.out.println(2<<3); 26 } 27 }
输出结果

三元运算符
1 package operator; 2 3 public class demo6 { 4 public static void main(String[] args) { 5 //三元运算符 6 //x?y:z 7 //如果x==true,则结果为y,否则结果为z 8 int score = 55; 9 String type = score < 60?"不及格":"及格"; 10 //if 11 System.out.println(type); 12 } 13 }
运行结果

包机制

https://www.bilibili.com/video/BV12J41137hu?p=31

用户交互和scannner

1 package operator; 2 3 import java.util.Scanner; 4 //导入Scanner 5 public class demo7 { 6 public static void main(String[] args) { 7 8 //创建一个扫描器对象,用于接收键盘数据 9 10 Scanner scanner = new Scanner(System.in); 11 12 System.out.println("使用next方式接收"); 13 14 //判断用户有没有输入字符串 15 if(scanner.hasNext()) 16 { 17 //使用next方式接收 18 String str = scanner.next(); 19 System.out.println("输出的内容为:"+str); 20 } 21 //凡是属于io流的类如果不关闭会一直占用资源,要用完就关掉 22 scanner.close(); 23 } 24 }
输出示例


1 package operator; 2 3 import java.util.Scanner; 4 5 public class demo8 { 6 public static void main(String[] args) { 7 8 //创建一个扫描器对象,用于接收键盘数据 9 10 Scanner scanner = new Scanner(System.in); 11 12 System.out.println("请输入数据"); 13 14 String str = scanner.nextLine(); 15 System.out.println("输出的内容为"+str); 16 17 scanner.close(); 18 } 19 }
输出结果为

浙公网安备 33010602011771号