重要的内容
1.分支
上篇遗留的逻辑运算符的内容
0.0逻辑运算符的断路原则
&& (与)||(或) !(非)
逻辑与的断路原则:
条件1&&条件2&&条件3 只要你有一个条件为false,整体的结果就为false
如果条件1为false,条件2和条件3就不执行了
逻辑或的断路原则:
条件1||条件2||条件3 只要你有一个条件为true,整体的结果就为true
如果条件1位true,条件1和条件2就不执行。
如果条件1为false,条件2执行,条件2位true,条件3就不执行了。
/**
* 逻辑断路原则
*/
public class Demo1 {
public static void main(String[] args) {
int num = 10;//声明了一个变量叫num,把10赋值给num
//条件1&&条件2
//3>4 && ++num>10
//false && ++num
//3>4 false 断路原则,如果出现了false,后面条件不执行了
boolean ret = (3 > 4) && (++num>15);
System.out.println(ret);//false
System.out.println(num);//11
/*
* ctrl+shift+? 多行注释的快捷键
* ctrl + ?单行注释的快捷键
* 逻辑或的断路原则
* */
int num2 = 20;
//++num2 > 20 这个代码没有执行
boolean ret1 = 4 > 3 || ++num2 > 20;
System.out.println(ret1);//true
System.out.println(num2);//20
}
}
1.分支
生活中的分支:人生的选择
程序中的分支:也是一个选择
1.1 if分支
语法格式:
if (条件) {
语句体
}
条件必须是布尔类型的数据(true或者false)
执行流程:如果条件为true的话,就执行条件后面大括号里面的内容。如果条件为false,就跳过大括号里面的内容去执行下面的代码。
/**
* if结构
*/
public class Demo2 {
public static void main(String[] args) {
int money = 9;
if (money > 10) {
System.out.println("可以买兰博基尼了");
}
System.out.println("程序结束");
}
}
1.2if-else分支
语法格式:
if (条件) {
语句体1
} else {
语句体2
}
执行流程:
当成程序运行到if-else之后,首先判断条件,如果条件为true的执行语句体1.
如果条件为false执行语句体2,两者只能选择其一。
/**
* if-else分支结构
*/
public class Demo3 {
public static void main(String[] args) {
int a = 19;
if (a >= 10) {
System.out.println("今天中午吃顿好的!泡面加蛋");
} else {
System.out.println("西北风搞起");
}
System.out.println("程序结束");
}
}
1.3if-else if 分支结构
语法格式:
if (条件1) {
语句体1
}else if(条件2) {
语句体2
} else if (条件3) {
语句体3
……
} else if(条件n) {
语句体n
} else {
语句体n+1
}
执行流程:
当成运行到if的时候,首先判断小括号里面条件,如果条件为tfalse就跳过语句体,执行下面的else if 的条件,直到条件为true,就输出打印,下面的条件就不用再判断了。
/**
* if-else if() 结构
*/
public class Demo4 {
public static void main(String[] args) {
/*
* 学生的成绩:
* 90-100:打印 秀儿
* 80-90:良好
* 70-80:一般
* 60-70:及格
* 60以下:叫家长
*
* */
int score = 70;//先定义一个变量 值为70
if (score >= 90 && score <= 100) {
System.out.println("秀儿");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("一般");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("叫家长");
}
}
}
再举个栗子
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("亲,请输入你的成绩:");
score = scanner.nextInt();//控制台输入数据,赋值给score这个变量了
//合法性的判断
if (score < 0 || score > 100) {
System.out.println("亲,你个逗逗,输错了哦!!!");
//退出程序
System.exit(0);
}
if (score >= 90 && score <= 100) {
System.out.println("秀儿");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("一般");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("叫家长");
}
}
}
1.4switch-case分支
也是一种选择,只要满足当前的条件就可以执行当前的操作了
switch(表达式) {
case 常量1:
语句体1;
break;
case 常量2:
语句体2;
break;
case 常量3:
语句体3;
break;
default:
语句体n;
break;
}
break:终止,打断的意思
import java.util.Scanner;
public class Demo7 {
public static void main(String[] args) {
char c = '0';
Scanner scanner = new Scanner(System.in);
System.out.println("客官请点菜哦!!!");
System.out.println("1.烤全猪");
System.out.println("2.烤全羊");
System.out.println("3.烤骆驼");
System.out.println("4.烤韭菜");
System.out.println("5.烤枸杞");
c = scanner.nextLine().charAt(0);
switch (c) {
case '1':
System.out.println("烤全猪,肥的流油。。。。");
break;
case '2':
System.out.println("烤全羊,2000一只");
break;
case '3':
System.out.println("烤骆驼,只吃驼峰......");
break;
case '4':
System.out.println("烤韭菜,没有吃过!!!");
break;
case '5':
System.out.println("烤枸杞,这个能火起来");
break;
default:
System.out.println("本店没有这个餐");
break;
}
}
}
1.5案例1
请插入您的卡,输出密码:
1.密码输对了
密码正确以后,请选择办理的业务:1.取款 2.存款 3.修改密码
1取款
2存款
3修改密码
4没有这个业务
2.密码输错了
import java.util.Scanner;
//银行卡取款的小案例 if- else
public class Demo9 {
public static void main(String[] args) {
int password = 123456;//卡的密码
int account = 100000;//卡里的钱
System.out.println("请插入您的卡输入密码:");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();//控制台输入的密码
if (number == password) {
//1密码正确的一步
//请选择办理的业务:1.取款 2存款 3修改密码
System.out.println("登录成功!!!");
System.out.println("请选择办理的业务:1.取款 2.存款 3.修改密码");
Scanner scanner1 = new Scanner(System.in);
int access = scanner1.nextInt();//选择办理业务
if (access == 1) {
//取款
//请输入取款的金额
System.out.println("请出入取款的金额:");
Scanner scanner2 = new Scanner(System.in);
int outMoney = scanner2.nextInt();//取款的金额
System.out.println("您的取款金额为:" + outMoney);
if (outMoney > account) {
System.out.println("余额不足");
}else {
int account1 = account - outMoney;//目前取款以后的余额
System.out.println("您卡里面的余额为:" + account1);
System.out.println("取款成功,请取卡,欢迎下次光临");
}
}else if (access == 2) {
//存款
System.out.println("请输入您存款的金额:");
Scanner scanner2 = new Scanner(System.in);
int inMoney = scanner2.nextInt();//存款的金额
int account3 = account + inMoney;
System.out.println("您卡里的余额为:" + account3);
System.out.println("存款成功,你走吧");
}else if (access == 3) {
//修改密码
System.out.println("请输入原始密码:");
Scanner scanner2 = new Scanner(System.in);
int word = scanner2.nextInt();//输入的原始密码
if (word == password) {
System.out.println("请输入新的密码:");
Scanner scanner3 = new Scanner(System.in);
int newPassword = scanner3.nextInt();//输入的新密码
password = newPassword;
System.out.println("密码修改成功!!!");
}else {
//原始密码输入错误
System.out.println("原始密码输入错误,此卡冻结");
}
}else {
System.out.println("没有这样的业务");
System.exit(0);//程序退出
}
} else {
//2密码错误的一步
System.out.println("密码错误,请重新输入");
}
}
}
1.6案例2
判断输入的一个年份是否是平年或者闰年
(能被400整除)或者 (能被4整除且不能被100整除)
import java.util.Scanner;
/**
* 判断是否是平年闰年
*/
public class Demo10 {
public static void main(String[] args) {
System.out.println("请输入一个年份:");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();//输入的一个年份
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
System.out.println(year + "是闰年");
} else {
System.out.println(year + "是平年");
}
}
}
1.7案例3
三个数,比较大小,输出最大的那个数
import java.util.Scanner;
//三个不同数比较大小,输出最大的数
public class Demo11 {
public static void main(String[] args) {
System.out.println("请输入三个数:");
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if (a > b && a > c) {
System.out.println(a);
} else if (b > a && b > c) {
System.out.println(b);
}else if(c > a && c > b) {
System.out.println(c);
}
}
}
1.8案例4
输入一个月份,打印对应的季节(春夏秋冬)
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
System.out.println("请输入一个月份:");
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
if (month <= 0 || month > 12) {
System.out.println("输入月份有误,请请重新输入!!!");
System.exit(0);
}
if (month >= 3 && month <= 5) {
System.out.println("春季");
}else if (month >= 6 && month <= 8) {
System.out.println("夏季");
}else if (month >= 9 && month <= 11) {
System.out.println("秋季");
}else if (month == 12 || month <= 2 && month >= 1) {
System.out.println("冬季");
}
}
}
我这里有几道基础题,大家勤加练习,熟练了也可以试试多重嵌套(注意代码规范哦!)
1、定义一个int类型的变量为x并赋值为5,如果x<10,则让x实现自增,并输出x最后的值。
2、随意输入一个数字a,若a>20,输出a的值。
3、定义一个int类型的变量成绩为59,如果成绩大于等于60,则输出成绩及格,否则,输出成绩不及 格。
4、编程输入整数a和b,若a或b大于100,则输出a,b的和与100的差,否则输出两数之和。
5、从键盘输入三角形的三边长,判断出这三边能否构成三角形
6、随便输入一个数,判断该数为奇数还是偶数;
7、写程序输出一个圆的面积;
8、根据输入的月份,判断该月份有多少天
9、输入员工登记A、B、C级,判断其是否加薪,加薪对应500、300、100(switch语句)
10、随意输入一个数字1-7,判断是周几,输入其他数字提示输入错误。(switch语句写)
补充知识点(Scanner的使用)
现在声明变量的时候,直接初始化的值。这个值是在咱们的代码中写好的。
使用Scanner能够在控制台手动输入,并捕获输入的值。捕获完以后的值,赋值给变量。
1.需要导包 import java.util.Scanner;
2.创建一个叫scanner对象的变量
Scanner sc = new Scanner(System.in);
3.使用sc这个对象获取控制台输入的数据,赋值给变量
获取int类型的数据:int num= sc.nextInt();
获取float类型的数据:float f1 = sc.nextFloat();
获取double类型的数据: double d1 = sc.nextDouble();
获取字符串类型数据:String s1 = sc.nextLine();
获取字符类型数据:Char c1 = sc.nextLine().charAt(0);
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
//scanner 是java给咱们封装好的
//控制台输入的事情
Scanner scanner = new Scanner(System.in);
//整数
//System.out.println("亲!!!请输入一个整数:");
//scanner获取控制台输入的整数
//int i = scanner.nextInt();
//小数
// System.out.println("亲!!!请输入一个小数:");
// //scanner获取控制台输入的小数
// double v = scanner.nextDouble();
// System.out.println(v);
//获取字符串的内容,中间可以加空格
// System.out.println("请输入一个字符串:");
// String s = scanner.nextLine();
// System.out.println(s);
//获取字符串的内容,中间不可以加空格
// System.out.println("请输入一个字符串:");
// String next = scanner.next();
// System.out.println(next);
//通过控制台获取字符
System.out.println("请输入字符:");
//按顺序是
char c = scanner.nextLine().charAt(0);
System.out.println(c);
}
}
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("亲,请输入你的成绩:");
score = scanner.nextInt();//控制台输入数据,赋值给score这个变量了
//合法性的判断
if (score < 0 || score > 100) {
System.out.println("亲,你个逗逗,输错了哦!!!");
//退出程序
System.exit(0);
}
if (score >= 90 && score <= 100) {
System.out.println("秀儿");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("一般");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("叫家长");
}
}
}