Day03Java基础之逻辑运算符,条件运算符

Day03

  1. 逻辑运算符
    (1) 包含:&,|,^异或,! &&,||
    (2) 逻辑运算符一般用于连接boolean类型的表达式或者值,结果为true或者false。
    (3) 结论:
    A. 逻辑与&,表示并且,遇到false则false;只有左右都为true,结果才为true。
    int x = 10;
    X > 5 & X < 15; //true & true = true
    B. 逻辑或|,表示或者or,遇到true则true;只有左右都为false,结果才为false。
    int x = 10;
    X > 11 | X < 9; //false & false = false
    C. 逻辑异或^,两边相同就为false,两边不同为true。
    D. 逻辑非!,非true就为false,非false就为true。
  2. 逻辑运算符&&与&的区别。
    (1) &&与&的区别:
    A. 最终结果是一样的。
    B. &&具有短路效果,左边是false,右边不执行。
int x = 3;
int y = 4;
System.out.println((++x == 3 ) & (++y == 4));   //false & false = false
System.out.println(“x = “ + x);  //x = 4
System.out.println(“y = “ + y);  //y = 5

int x = 3;
int y = 4;
System.out.println((++x == 3 ) && (++y == 4)); //false & false = false,只执行左边
System.out.println(“x = “ + x);  //x = 4
System.out.println(“y = “ + y);  //y = 4

(2) ||与|的区别:
A. 最终结果是一样的。
B. ||具有短路效果,左边是true,右边不执行。
3. 位运算符
(1) 包括:&,|,^,~,>>,>>>,<<
(2) 位运算符的基本用法:
A. 位与&,有0则0
例:6 & 3 =2
110
& 011
= 010 =2
B. 位或|,有1则1
C. 位异或^,相同则0,不同则1
D. 按位取反~
例~6 = -7
00000000 00000000 00000000 00000110 //6的原码反码补码本身
11111111 11111111 11111111 11111001 //对6取反,为补码
11111111 11111111 11111111 11111000 //补码-1,得到反码
10000000 00000000 00000000 00000111 //反码除符号位,取反,得到-7
4. 位异或^运算符的特点及面试题
(1) 特点:一个数据对另一个数据异或两次,结果不变
5 ^ 10 ^ 10 = 5;
5 ^ 10 ^ 5 = 10;
(2) 面试题:
A. 请实现两个整数变量的交换(不需要定义第三方变量)
int x = 5;
int y = 10;
方法一:该方法有弊端,如果数值很大,相加会超出int范围
x = x + y; //10 + 5 = 15
y = x – y; //15 - 10 = 5
x = x – y; //15 - 5 = 10方法二:
方法二:异或方法
x = x ^ y;
y = x ^ y; // 10 ^ 5 ^ 10 = 5
x = x ^ y; // 10 ^ 5 ^ 5 = 10
5. 位运算符的基本用法2及面试题
(1) >>,>>>,<<的用法
A. <<:左移 左边最高位丢弃,右边补齐0;向左移动几位就是乘以2的几次幂。
12 << 1 = 24;
12 << 2 = 48;
00000000 00000000 00000000 00001100 12的补码
向左移1位 00000000 00000000 00000000 00011000 24的补码
向左移2位 00000000 00000000 00000000 00110000 48的补码

B. >>:右移 最高位是0,左边补齐0;最高位是1,左边补齐1;向右移动几位就是除以2的几次幂。
00000000 00000000 00000000 00001100 12的补码
向右移1位 00000000 00000000 00000000 00000110 6的补码
向右移2位 00000000 00000000 00000000 00000011 3的补码
C. >>>:无符号右移 无论最高位是0还是1,左边补齐0;
(2) 最有效率的算出2 * 8的结果
System.out.println(2 << 3);

  1. 三元运算符的基本用法
    (1) 格式:(关系表达式)?表达式1 : 表达式2;
int x = 10;
int y = 5;
int z;
z = (x > y) ? x : y;       //z = 10

(2) 执行流程
7. 三元运算符的练习
(1) 比较两个整数是否相同

int x = 10;
int y = 5;
boolean b = (x == y) ? true : false;    //结果为false
boolean b = (x == y); //结果为false

(2) 获取三个数中的最大值

int a = 10;
int b = 20;
int c = 30;
//先比较任意两个数的值,找出这两个数的最大值
int temp = (a > b) ? a : b;
//用前两个数的最大值与第三个数比较
int max = (temp > c) ? temp : c;
  1. 键盘录入基本格式讲解
    (1) 作用
    A. 为了让程序的数据更符合开发的数据
    B. 让程序更灵活
    (2) 如何实现录入
    A. 导包
    格式: import java.util.Scanner;
    位置:在class上面。
    B. 创建键盘录入对象
    格式:Scanner sc = new Scanner(System.in);
    C. 通过对象获取数据
    格式:int x = sc.nextInt();

(3) 案例
A. 键盘录入1个整数,并输出到控制台
B. 程序:

import java.util.Scanner;
class Demo_Scanner	{
		public static void main(String[] args)	{
				Scanner sc = new Scanner(System.in);  //创建键盘录入对象
				System.out.println(“请输入一个整数”);
				Int x = sc.nextInt();
				System.out.println(x);
	}
}

C. 键盘录入2个整数,并输出到控制台

import java.util.Scanner;
class Demo_Scanner	{
	public static void main(String[] args)	{
			Scanner sc = new Scanner(System.in);
			System.out.println(“请输入第一个整数”);
			int x = sc.nextInt();
			System.out.println(x);
			
			System.out.println(“请输入第二个整数”);
			int y= sc.nextInt();
			System.out.println(y);

		}
	}

  1. 键盘录入练习
    (1) 键盘录入2个数据,并对其求和,输出结果
import java.util.Scanner;
class Test2_Scanner	{
		public stsatic void main(String[] args)	{
				Scanner sc = new Scanner(System.in);  //创建键盘录入对象
				System.out.println(“请输入第一个整数”);  
				int x = sc.nextInt();   
				System.out.println(“请输入第二个整数”);
				int y = sc.nextInt();    
				int sum = x + y;
				System.out.println(sum);
			}
		}

(2) 键盘录入2个数据,获取两个数据的最大值

import java.util.Scanner;
class Test3_Scanner	{
			public stsatic void main(String[] args)	{
				Scanner sc = new Scanner(System.in);  //创建键盘录入对象
				System.out.println(“请输入第一个整数”);  
				int x = sc.nextInt();   
				System.out.println(“请输入第二个整数”);
				int y = sc.nextInt();    
				int max = (x > y) ? x : y;
				System.out.println(max);
			}
		}

				

  1. 键盘录入练习2
    (1) 键盘录入2个数据,比较这两个数据是否相等
import java.util.Scanner;
class Test4_Scanner	{
		public static void main(String[] args)	{
			Scanner sc = new Scanner(System.in);
			System.out.println(“请输入第一个数值”);
			int x = sc.newInt();
   			System.out.println(“请输入第二个数值”);
			int y = sc.newInt();

			boolean b = (x == y);
			System.out.println(b);
		}
	}

(2) 键盘录入3个数据,获取这3个数据的最大值

import java.util.Scanner;
class Test5_Scanner	{
		public static void main(String[] args)	{
			Scanner sc = new Scanner(System.in);
			System.out.println(“请输入第一个数值”);
			int x = sc.newInt();
   			System.out.println(“请输入第二个数值”);
			int y = sc.newInt();
			System.out.println(“请输入第一个数值”);
			int z = sc.newInt();
			
			int temp = (x > y) ? x : y;
            int max = (temp > z) ? temp : z;
			System.out.println(max);
		}
	}

  1. 顺序结构语句
    (1) 什么是流程控制语句:可以控制程序的执行
    (2) 流程控制语句的分类:
    A. 顺序结构
    B. 选择结构
    C. 循环结构
    (3) 执行流程:从上往下,依次执行
  2. 选择结构if语句格式1及使用
    (1) 选择结构分类
    A. if语句
    B. switch语句
    (2) if语句的几种格式
    A. 格式1:
    if (比较表达式) { //先计算比较表达式的值,看其返回值是true或false
    语句体; //若为true,就执行语句体,false就不执行语句体
    }
    注意事项:
    a. 比较表达式,无论简单还是复杂,必须为boolean类型。
    b. if 语句如果语句体是一条语句,则大括号可以省略,如果是多条语句,不可以省略,建议一直不要省略。
    c. 一般来说,有左大括号就if语句后没有分号,有分号就没有左大括号。
    B. 格式2:
    if (比较表达式) { //先计算比较表达式的值,看其返回值是true或false
    语句体1; //若为true,就执行语句体1,false就不执行语句体2
    } else { //注意:else后面没有比较表达式
    语句体2;
    }
    if语句格式2与三元运算符的相互转换问题:
    a. 三元运算符可以实现的都可以用if语句实现,反之不成立。
    b. 当if语句控制的操作是一个输出语句的时候不能用三元改进,因为三元运算符是一个运算符,运算符操作完毕就应该有一个结果,而不是一个输出。
    C. 格式3:
    if (比较表达式1) { //计算比较表达式1的值,看其返回值是true或false
    语句体1; //若为true,就执行语句体1,false就不接着判断表达式2
    } else if (比较表达式2){ //计算比较表达式2的值
    语句体2;
    } else if (比较表达式3){ //计算比较表达式3的值
    语句体3;
    }
    ……
    } else {
    语句体n+1;
    }
  3. 练习选择结构if格式3
    (1) 键盘录入一个成绩,判断并输出成绩等级。
    90-100 优;80-89 良;70-79 中;60-69 及;0-59 差;
import java.util.Scanner;
class ChengJi	{
	public static void main(String[] args)	{
		Scanner sc = new Scanner(System.in);
		System.out.println(“请输入学生成绩范围在0到100之间”);
		Int x = sc.newInt();
    	if (90 <= x && x <= 100)	{
			System.out.println(“优”);
		}else if (80 <= x && x <= 89)	{
			System.out.println(“良”);
		}else if (70 <= x && x <= 79)	{
			System.out.println(“中”);
		}else if (60 <= x && x <= 69)	{
			System.out.println(“及”);
		}else if (0 <= x && x <= 59){
			System.out.println(“差”);
		} else {
			System.out.println(“成绩录入错误”);
				}
		}
	}

(2) 键盘录入一个x的值,计算除y,并输出。
X >= 3 y = 2 * x + 3;
-1 < x < 3 y = 2 * x;
x <= -1 y = 2 * x – 1;

程序:

import java.util.Scanner;
class Test {
	public static void main(String[] args)	{
		Scanner sc = new.Scanner(System.in);
		System.out.println(“输入x”);
		int x = sc.newInt();
		int y = 0;
		if (x >= 3)	{
			y = 2 * x + 3;
		}else if (-1 < x < 3)	{
			y = 2 * x;
		}else if (x <= -1) {
			y = 2 * x – 1;
		}
	System.out.println(y);
	}
}
  1. if语句的嵌套使用
    获取三个数据中的最大值:
class Test_IfIf	{
		public static void main(String[] args)	{
		int a = 10;
		int b = 20;
		int c = 30;
		
		if (a > b)	{
			if (a	> c)	{
				System.out.println(a + “是最大值”);
			}else {
				System.out.println(c + “是最大值”);
			}else {  //b>=a
			if (b	> c)	{
				System.out.println(b + “是最大值”);
			}else {
				System.out.println(c + “是最大值”);
			}
		}
	}
}
  1. switch语句的格式及其解释
    (1) 格式:
    switch (表达式){
    //基本数据类型可以接收byte,short,char,int.。注意long不可以
    case 值1 ://引用数据类型可以接收枚举(JDK1.5),String字符串(JDK1.7)
    break;
    case 值2 :
    语句体2;
    break;

    default :
    语句体n+1;
    break;
    }
    (2) Switch 语句格式解释
    (3) 执行流程:先计算表达式的值;然后与case后面进行匹配,如果有就执行对应的语句,否则执行default控制的语句。
    (4)
	String name = “张三”;
	String gender = “男士”;
	switch (gender) {
	case “男士”:
		System.out.println(name + “是一位” + gender + “喜欢游戏”);
	break;
	case “女士”:
		System.out.println(name + “是一位” + gender + “喜欢购物”);
	break;
	default:
		System.out.println(name + “是一位” + gender + “喜欢美容”);
	break;
} 

  1. switch语句的练习
    输入一个值,输出是星期几
import java.util.Scanner;
class Test_Switch	{
		public static void main(String[] args) 	{
			Scanner sc = new Scanner(System.in);
			System.out.println(“请输入一个值”);
			int x = src.newInt();
			switch (x)	{
			casae 1:
				System.out.println(“今天是周一”);
			break;
			casae 2:
				System.out.println(“今天是周二”);
			break;
			casae 3:
				System.out.println(“今天是周三”);
			break;
			casae 4:
				System.out.println(“今天是周四”);
			break;
			casae 5:
				System.out.println(“今天是周五”);
			break;
			casae 6:
				System.out.println(“今天是周六”);
			break;
			casae 7:
				System.out.println(“今天是周日”);
			break;
			default :
				System.out.println(“输入错误”);
			break;
			}
		}
}
  1. switch语句的注意事项
    (1) case 后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的。
    (2) default可以省略,都是不建议省。当case值为固定时可以省。
    (3) break,最后一个可以省,但是不建议省。其他省略会出现case穿透。
    (4) default可以在任意位置,但是建议放在最后。
    (5) switch语句的结束条件:遇到break就结束了;执行到右大括号就结束了。
  2. switch语句的练习
    (1) 看程序写结果:
int x = 2;
int y = 3;
switch(x)	{
		default:
			y++;
			break;
		case 3:
			y++;
		case 4:
			y++;
			System.out,println(“y =” + y);

先执行case,不匹配就再执行default,最后,y++ = 4;break结束。
(2) 看程序写结果:

int x = 2;
int y = 3;
switch(x)	{
		default:
			y++;
		case 3:
			y++;
		case 4:
			y++;
			System.out,println(“y =” + y);

先执行case,不匹配就再执行default,最后,y++ = 4;无break继续执行两次y++。碰到右大括号结束,得到y = 6.
19. 选择结构if 和switch 的区别
(1) switch 建议判断固定值时用
(2) if 建议判断区间或范围时使用
(3) 例子:键录入月份,输出季节。
程序1:用if 语句

import java.util.Scanner;
class Test {
	public static void main(String[] args)	{
		Scanner sc = new.Scanner(System.in);
		System.out.println(“输入月份”);
		int month = sc.newInt();
		if (month = 12 || month = 1 ||month = 2)	{
		System.out.println(“没有对应月份”);
		}else if (3 <= month && month <= 5)	{
			System.out.println(month + “是春季”);
		}else if (6 <= x && x <= 8)	{
			System.out.println(month + “夏季”);
		}else if (9 <= x && x <= 11)	{
			System.out.println(month + “秋季”);
		}else 
			System.out.println(month + “冬季”);
		} 
	}
}

程序2:用switch 语句

import java.util.Scanner;
class Test_Switch	{
		public static void main(String[] args) 	{
			Scanner sc = new Scanner(System.in);
			System.out.println(“请输入月份”);
			int month = src.newInt();
			switch (month)	{
			casae 3:
			casae 4:
			casae 5:
				System.out.println(month + “是春季”);
			break;
			casae 6:
			casae 7:
			casae 8:
				System.out.println(month + “是夏季”);
			break;
			casae 9:
			casae 10:
			casae 11:
				System.out.println(month + “是秋季”);
			break;
			casae 12:
			casae 1:
			casae 2:
				System.out.println(month + “是冬季”);
			break;
			default :
				System.out.println(“没有对应月份”);
			break;
			}
		}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值