逻辑运算符的基本用法演示
逻辑非 ! :非true则false,非false则true,偶数个非不改变本身.
class Operator_1 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 30;
//逻辑与 & ,是并且,and的意思.
System.out.println(a < b & b < c); //true & true = true
System.out.println(a > b & b < c); //false & true = false
System.out.println(a < b & b > c); //true & false = false
System.out.println(a > b & b > c); //false & false = false
//结论:遇false则false.
System.out.println("-------------------------");
//逻辑或 | ,是或者,or的意思.
System.out.println(a < b | b < c); //true | true = true
System.out.println(a > b | b < c); //false | true = true
System.out.println(a < b | b > c); //true | false = true
System.out.println(a > b | b > c); //false | false = false
//结论:遇true则true.
System.out.println("-------------------------");
//逻辑异或 ^ .
System.out.println(a < b ^ b < c); //true ^ true = false
System.out.println(a > b ^ b < c); //false ^ true = true
System.out.println(a < b ^ b > c); //true ^ false = true
System.out.println(a > b ^ b > c); //false ^ false = false
//结论:两边相同则false,两边不同则true.
System.out.println("-------------------------");
//逻辑非 ! .
System.out.println(!true); //非true即false
System.out.println(!!true); //非非true即true
//结论:非true则false,非false则true,偶数个非不改变本身.
}
}
&&与&的区别
&&具有短路效果,左边是false,右边就不执行.
class Operator_1 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 30;
System.out.println(a < b && b < c); //true && true = true
System.out.println(a > b && b < c); //false && true = false
System.out.println(a < b && b > c); //true && false = false
System.out.println(a > b && b > c); //false && false = false
/*&&和&的区别
a.最终结果一样
b.&&具有短路效果,左边是false,右边就不执行.*/
System.out.println(a < b && b < c); //true && true = true
System.out.println(a > b && b < c); //false && true = false
System.out.println(a < b && b > c); //true && false = false
System.out.println(a > b && b > c); //false && false = false
int x = 3;
int y = 4;
//System.out.println(++x == 3 & ++y == 4); //遇false则false
//System.out.println("x = " + x + ",y = " + y); //x = 4,y = 5
System.out.println(++x == 3 && ++y == 4); //左边是false,右边不执行,左边是true,右边一定要执行.
System.out.println("x = " + x + ",y = " + y); //x = 4,y = 4
System.out.println("-------------------------");
/*||与|的区别
a.最终结果一样
b.||具有短路效果,左边是true,右边就不执行.*/
System.out.println(a < b || b < c); //true || true = true
System.out.println(a > b || b < c); //false || true = true
System.out.println(a < b || b > c); //true || false = true
System.out.println(a > b || b > c); //false || false = false
System.out.println(++x == 3 | ++y == 4); //遇true则true
System.out.println("x = " + x + ",y = " + y); //x = 4,y = 5
System.out.println(++x == 3 || ++y == 4); //左边是true,右边不执行,左边是false,右边一定要执行.
System.out.println("x = " + x + ",y = " + y); //x = 4,y = 5
}
}
位运算符的用法1
取反~ :按位取反.
class Operator_2 {
public static void main(String[] args) {
/*
* 位与&:有0则0;
* 位或|:有1则1;
* 位异或^:相同则0,不同则1;
* 取反~ :按位取反.
*/
System.out.println(7 & 3); //输出结果3
System.out.println(7 | 3); //7
System.out.println(7 ^ 3); //4
System.out.println(~7); //-8
//**位异或运算符的特点----一个数据对另一个数据异或两次,该数本身不变.**
System.out.println(6 ^ 3 ^ 3); //6
System.out.println(6 ^ 3 ^ 6); //3
//练习题:实现两整数变量间的转换
int x = 3;
int y = 6;
//需用第三方变量 (软件开发中建议常用方法)
int temp;
temp = x;
x = y;
y = temp;
System.out.println("x = " + x + ",y = " + y);
System.out.println("---------------");
//不用定义第三方变量.有弊端,容易超出int的取值范围.
x = x + y; //3 + 6 = 9
y = x - y; //9 - 6 = 3
x = x - y; //9 - 3 = 6
System.out.println("x = " + x + ",y = " + y);
System.out.println("---------------");
//不用定义第三方变量.
x = x ^ y; //3 ^ 6
y = x ^ y; //3 ^ 6 ^ 6
x = y ^ x; //3 ^ 6 ^ 3
System.out.println("x = " + x + ",y = " + y);
}
}
位运算符的用法2
无符号右移>>>:无论最高位是1还是0,左边补齐0.
class Operator_3 {
public static void main(String[] args) {
//左移,向左移动几位就是乘以2的几次幂.
System.out.println(8<<1); //16
System.out.println(8<<2); //32
//右移,向右移动几位就是除以2的几次幂.
System.out.println(8>>1); //4
System.out.println(8>>2); //2
}
}
三元运算符的基本格式—-(关系表达式) ? 表达式1 : 表达式2;
class Operator_4 {
public static void main(String[] args) {
//获取两个数的最大值
int x1 = 10;
int y1 = 20;
int max;
max = (x1 > y1) ? x1 : y1; //输出结果max = 20
System.out.println("max = " + max);
//比较两个整数是否相同
int a = 10;
int b = 20;
boolean c = (a == b) ? true : false;
boolean c1 = (a == b); //false
System.out.println("c = " + c);
System.out.println("c1 = " + c1);
//求三个整数中的最大值
int x = 10;
int y = 20;
int z = 30;
int temp = (x > y) ? x : y;
int max1 = (temp > z) ? temp : z; //max1 = 30
System.out.println("max1 = " + max1);
}
}
实现键盘录入的基本格式
通过对象获取数据 格式:int x = nestInt();
import java.util.Scanner;
class Scanner_1 {
public static void main(String[] args) {
//录入一个整数
Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入一个整数");
int x = sc.nextInt(); //将键盘录入的数据存储在X中
System.out.println("x = " + x);
System.out.println("------------------- " );
//录入两个整数
//Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入一个整数");
int y = sc.nextInt(); //将键盘录入的数据存储在y中
System.out.println("y = " + y);
System.out.println("请输入第二个整数"); //创建键盘录入对象
int z = sc.nextInt();
System.out.println("z = " + z); //将键盘录入的数据存储在z中
}
}
练习题
import java.util.Scanner;
class Test_1 { //导入包中的类Scanner
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("输入第一个整数");
int x = sc.nextInt(); //将键盘录入的数据存储在x中
System.out.println("输入第二个整数");
int y = sc.nextInt(); //将键盘录入的数据存储在y中
System.out.println("输入第三个整数");
int w = sc.nextInt(); //将键盘录入的数据存储在w中
//练习一
int z = x + y; //将x和y求和
System.out.println("z = " + z);
//练习二
int max = (x > y) ? x : y; //比较x和y值的大小
System.out.println("max = " + max);
//练习三
boolean q = (x == y); //比较两个数值是否相等
System.out.println("q = " + q);
//练习四 `
int temp = (x > y) ? x : y; //比较三个数值取最大值
int max2 = (temp > w) ? temp : w;
System.out.println("max2 = " + max2);
}
}
顺序结构格式:按顺序依次输出.
class Order { //Order顺序
public static void main(String[] args) {
System.out.println("Hello World1111!");
System.out.println("Hello World4444!");
System.out.println("Hello World2222!");
System.out.println("Hello World3333!");
}
}
选择结构 if 语句格式及其使用
if 语句的几种格式
格式一: if(比较表达式) {
语句体;
}
执行流程
如果是false就不执行语句体.
格式二: if(条件表达式){
语句体1;
}else {
语句体2;
}
注意:else后面是没有条件表达式的,只有 if 后面有.
格式三: if(比较表达式1){
语句体1;
}else if(比较条件式2) {
语句体2;
}else if(比较表达式3) {
语句体3;
}
....
else {
语句体n+1;
}
注意:最后一个else可以省略,但是建议不要省略,可以对其范围外的错误值进行提示.
格式一演示:
class Demo_If1 {
public static void main(String[] args) {
int age = 17;
if (age < 18) {
System.out.println("未成年人禁止入内");
}
System.out.println("主要看年龄,走吧");
}
}
格式二演示:
class Demo_If2 {
public static void main(String[] args) {
int num = 1;
if (num == 1) {
System.out.println("美容女士请进");
}else{
System.out.println("男士止步");
}
}
}
格式三演示;
class Demo_If3 {
public static void main(String[] args) {
int num = 2;
if (num == 1) {
System.out.println("美容女士请进");
}else if (num == 0) {
System.out.println("男士止步");
}else if (num == 2) {
System.out.println("宠物狗禁止入内");
}else {
System.out.println("无法识别性别");
}
}
}
案例演示
一:获取两个数据中的最大值;
class Test_If1 {
public static void main(String[] args) {
int x = 5;
int y = 10;
int z;
if (x > y) {
z = x;
}
else {
z = y;
}
System.out.println(y);
}
}
二:判断一个数据是奇数还是偶数.
判断条件:x % 2 == 0 或者 (x & 1) == 1.
class Test_If2 {
public static void main(String[] args) {
int x = 10;
if (x % 2 == 0) { // 判断条件也可以是 (x & 1) == 1
System.out.println(x + "是一个偶数");
}else {
System.out.println(x + "是一个奇数");
}
}
}
if 语句的格式二与三元运算符的相互转换问题
用 if 语句格式二和三元运算符输出同一结果
class Test_If3 {
public static void main(String[] args) {
//if 语句结构
int x = 10;
int y = 20;
int z;
if (x > y){
z = x;
System.out.println(x + "是最大值");
}else {
z = y;
System.out.println(y + "是最大值");
}
System.out.println(z);
//三元运算符结构
int a = 10;
int b = 20;
int c = (a > b) ? a : b;
System.out.println(c1);
}
}
选择结构 if 语句格式三练习
练习一:键盘录入一个成绩,判断并输出成绩的等级,90-100优秀;80-89良好;70-79中等;60-69及格;0-59差.
//练习一
import java.util.Scanner; //导入包中的类
class Test_If4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入一名学生成绩");
int x = sc.nextInt(); //将键盘录入的数据存储在x中
if (x > 90 && x < 100){
System.out.println("优秀");
}else if (x > 80 && x < 89){
System.out.println("良好");
}else if (x > 70 && x < 79){
System.out.println("中等");
}else if (x > 60 && x < 69){
System.out.println("及格");
}else if (x > 0 && x < 59){
System.out.println("差");
}else {
System.out.println("输入成绩有误");
}
}
}
练习二:键盘录入X的值,计算出y的值并输出,x>=3,y=2*x+1;-1
switch 语句的格式
switch (表达式) {
case 值1:
语句体1;
break;
case 值2:
语句体2;
break;
...
default:
语句体n + 1;
break;
}
执行流程
然后和case后面的值进行匹配,如果有就执行后面对应的语句,如果没有就执行default后面控制的语句.
//switch格式练习
class Demo_switch1 {
public static void main(String[] args) {
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 + "爱好狗");
}
}
}
switch语句的格式解释
注意事项
选择结构switch语句的练习题
给定一个整数值,输出对应的星期几.
//给定一个整数值,输出对应的星期几.
class Test_switch1 {
public static void main(String[] args) {
int week = 8;
switch (week){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("没有对应的星期");
}
}
}
看程序,写结果.
//看程序,写结果.
class Test_switch2 {
public static void main(String[] args) {
int x = 2;
int y = 3;
switch (x) {
case 2:
y++;
break;
case 3:
y++;
break;
case 4:
y++;
break;
default:
y++; //输出结果4
}
System .out.println("y = " + y);
}
}
//看程序,写结果.
int x = 2;
int y = 3;
switch (x) { //switch语句的结束条件是遇到break就结束了或者执行到switch的有大括号就结束.
default:
y++;
case 3:
y++;
case 4:
y++; //输出结果6
}
System .out.println("y = " + y);
}
}
练习 (用if和switch语句分别实现)
键盘录入月份,输出对应的季节.
/* 键盘录入月份,输出对应的季节.
3,4,5春天;
6,7,8夏天;
9,10,11秋天;
12,1,2冬天*/
import java.util.Scanner; //导出包中的类
class Test_IfSwitch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入一个月份值");
int mouth = sc.nextInt(); //将创建的数据存储在x中
//if结构语句
if (mouth >= 3 && mouth <= 5) {
System.out.println(x + "月是春天");
}else if (mouth >= 6 && mouth <= 8) {
System.out.println(x + "月是夏天");
}else if (mouth >= 9 && mouth <= 11) {
System.out.println(x + "月是秋天");
}else if (mouth >= 3 && mouth <= 5) {
System.out.println(x + "月是冬天");
}else if(mouth > 12 || mouth < 1){
System.out.println("没有对应的月份");
}
}
}
//switch结构语句
switch (mouth) {
case 3:
case 4:
case 5:
System.out.println(mouth + "月是春天");
break;
case 6:
case 7:
case 8:
System.out.println(mouth + "月是夏天");
break;
case 9:
case 10:
case 11:
System.out.println(mouth + "月是秋天");
break;
case 12:
case 1:
case 2:
System.out.println(mouth + "月是冬天");
break;
default:
System.out.println("没有对应的月份");
}
}
}