流程控制语句
流程控制语句:
- 顺序结构
- 分支结构
- 循环结构
一、 顺序结构:默认的从上到下的执行结构。
二、 分支结构
分为if语句和switch语句两种。
2.1 if语句
if语句的三种格式:
第一种格式:
if (关系表达式){
语句体; //即关系表达式为true才执行语句体
}
第二种格式:
if (关系表达式){
语句体1;
}else{
语句体2;
}
第三种格式:
if (关系表达式){
语句体1;
}else if{
语句体2; //从上往下依次判断,一个为真就执行并结束
}
…
else{
语句体n+1;
}
2.1.1 第一种格式 if
注意要点:
- 大括号的开头可以另起一行书写,但是建议写在第一行的末尾;
- 在语句体中,如果只有一句代码,大括号可以省略不写(定义变量属于两句:一句定义,一句赋值,这样会报错);
- 如果对一个布尔类型的变量进行判断,不要用==号。可以只在括号里写该变量,反正是布尔类型,其值只有true和false,就别判断啦。
package ketang;
import java.util.Scanner;
public class ifDemo1 {
public static void main(String[] args) {
// 酒量大于2斤,老丈人给回应,否则无
Scanner sc = new Scanner(System.in);
System.out.println("请输入酒量:");
int wine = sc.nextInt();
if (wine > 2){
System.out.println("小伙子不错!");
}
}
}
2.1.2 第二种格式 if else
package ketang;
import java.util.Scanner;
public class ifDemo2 {
public static void main(String[] args) {
/*
键盘录入一个整数,表示身上的钱
如果大于等于100块,就是网红餐厅。
否则,就吃经济实惠的沙县小吃。
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入您身上的钱数:");
int money = sc.nextInt();
if (money >= 100){
System.out.println("去网红餐厅!");
}else {
System.out.println("吃沙县小吃!");
}
}
}
package test;
import java.util.Scanner;
public class test3 {
public static void main(String[] args) {
/*
假设某影院售卖了100张票,票的序号为1~100.
其中奇数票号坐左侧,偶数票号坐右侧。
键盘录入一个整数表示电影票的票号。
根据不同情况,给出不同的提示:
如果票号为奇数,那么打印坐左边;
如果票号为偶数,那么打印坐右边。
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入电影票号:");
int a = sc.nextInt();
if (a >= 0 && a <= 100){
if (a % 2 == 1){
System.out.println("请坐在左边。");
}else {
System.out.println("请坐在右边。");
}
}
}
}
2.1.3 第三种情况
package ketang;
import java.util.Scanner;
public class ifDemo3 {
public static void main(String[] args) {
/*
根据不同的分数送不同的礼物。
如果是95~100分,送自行车一辆;
如果是90~94分,游乐场玩一天;
如果是80~89分,送变形金刚一个
如果是80分,揍一顿。
*/
//注意:分数在0-100之间才属于合理数据!!!
Scanner sc = new Scanner(System.in);
System.out.println("请输入小明的成绩:" );
int score = sc.nextInt();
if (score >= 0 && score <= 100){
if (score >= 95 & score <= 100){
System.out.println("送自行车一辆!");
}else if (score >= 90 & score <= 94){
System.out.println("游乐场玩一天!");
}else if (score >= 80 & score <= 89){
System.out.println("送变形金刚一个!");
}else{
System.out.println("挨揍吧你!");
}
}else {
System.out.println("录入成绩不合法!");
}
}
}
2.2 switch语句
2.2.1 switch语句的基本用法
switch (表达式){ //这里表达式会得到一个值
case 值1: //得到的值和case后面的值依次对比看执行哪个
语句体1;
break; //遇到break时跳出循环
case 值2:
语句体2;
break;
…
default: //上面都不能执行时则执行default
语句体n+1;
break;
}
package ketang;
public class SwitchDemo1 {
public static void main(String[] args) {
//兰州拉面、武汉热干面、北京炸酱面、陕西油泼面
String noodles = "海鲜拉面";
switch (noodles){
case "兰州拉面":
System.out.println("吃拉面!");
break;
case "武汉热干面":
System.out.println("吃热干面!");
break;
case "北京炸酱面":
System.out.println("吃炸酱面!");
break;
case "陕西油泼面!":
System.out.println("吃油泼面!");
break;
default:
System.out.println("吃泡面!");
}
}
}
2.2.2 switch语句的扩展
- default位置可随便写,也可以省略。但建议写在最后,并且不要省略。
- case穿透:没有break导致的。当对应的结束后,没有break,就会接着执行下一个case里的内容。最后直到遇到break或是遇到大括号为止。
- switch和if第三种语句各自使用的不同:if第三种格式更多用于对范围的判断;而switch更倾向于选择其一。
- switch的新特性:出现在JDK12以后。不用写break啦,而且看起来更整洁好看。
传统格式:
public class SwitchDemo2 {
public static void main(String[] args) {
int number = 1;
switch (number){
case 1:
System.out.println("一");
break;
case 2:
System.out.println("二");
break;
case 3:
System.out.println("三");
break;
default:
System.out.println("没有该选项");
}
}
}
新特性可以写成这样:
public class SwitchDemo2 {
public static void main(String[] args) {
int number = 1;
switch (number){
case 1 -> System.out.println("一");
case 2 -> System.out.println("二");
case 3 -> System.out.println("三");
default -> System.out.println("没有该选项");
}
}
}
简化写法还有:
一个case后跟好几个选项共同输出一个答案,可以这样写:
Test1:
package test;
import java.util.Scanner;
public class test6 {
public static void main(String[] args) {
/*
休息日和工作日 需求:
键盘录入星期数输出工作日、休息日 (1-5)工作日,(6-7)休息日。
*/
Scanner sc = new Scanner(System.in);
System.out.println("请键入星期数:");
int week = sc.nextInt();
switch (week){
case 1,2,3,4,5:
System.out.println("工作日");
break;
case 6,7:
System.out.println("休息日");
default:
System.out.println("录入不合法");
}
}
}
与新特性结合,再简化:
public class test6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请键入星期数:");
int week = sc.nextInt();
switch (week){
case 1,2,3,4,5 -> System.out.println("工作日");
case 6,7 -> System.out.println("休息日");
default -> System.out.println("录入不合法");
}
}
Test2:
在实际开发中,如果我们需要在多种情况下选择其中一个,就可以使用switch语句当我们拨打了某些服务电话时,一般都会有按键选择。
假设现在我们拨打了一个机票预定电话
电话中语音提示:
1机票查询
2机票预定
3机票改签
4退出服务
其他按键也是退出服务。请使用swtich模拟该业务逻辑。
package test;
import java.util.Scanner;
public class test7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个按键代表您要办理的业务:");
int key = sc.nextInt();
if (key >= 0 && key <= 9){
switch (key){
case 1 -> System.out.println("机票查询");
case 2 -> System.out.println("机票预订");
case 3 -> System.out.println("机票改签");
default -> System.out.println("退出服务");
}
}else {
System.out.println("输入不合法");
}
}
}
三、循环结构
循环结构一共三类:
- for循环
- while循环
- do…while循环
3.1 for循环
package test;
public class test8 {
public static void main(String[] args) {
/*
在实际开发中,需要重复执行的代码,会选择循环实现。
玩游戏的时候,如果网不好那么会经常断线重连
那么断线重连这个业务逻辑就需要重复执行
假设现在公司要求,断线重连的业务逻辑最多只写5次。请用代码实现。
备注:断线重连的业务逻辑可以用输出语句替代
*/
for(int i = 1;i <= 5;i++){
System.out.println("第"+ i +"次断线重连中……");
}
}
}
for循环扩展知识:
- 求和变量不能定义在循环内,这样该变量只在循环所属的大括号内有用;
- 并且,在循环内的求和变量,也只在当前的循环中是有用的。i=1时定义的sum变量,在本次循环结束后就被内存清掉了;下一次i=2循环时会再定义一次。
package test;
public class test9 {
public static void main(String[] args) {
// 累加:求1-100的和
int sum = 0;
for(int i = 1; i <= 100; i++){
sum += i;
}
System.out.println(sum);
}
}
与if语句的结合:
public class test9 {
public static void main(String[] args) {
// 累加:1-100内的偶数和
int sum = 0;
for(int i = 1; i <=100; i++){
if(i % 2 == 0){
sum += i;
}
}
System.out.println(sum);
}
}
import java.util.Scanner;
public class test10 {
public static void main(String[] args) {
/*
键盘录入两个数字,表示一个范围。
统计这个范围中,能被3整除,又能被5整除数字有多少个?
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入表示范围开始的数:");
int a = sc.nextInt();
System.out.println("请输入表示范围结束的数:");
int b = sc.nextInt();
int score = 0;
for(int i = a;i <= b;i++){
if (i % 3 == 0 && i % 5 == 0){
score++;
}
}
System.out.println("符合要求的数一共有"+ score +"个");
}
}
3.2 While循环
格式:
初始化语句;
While(条件控制语句){
循环体语句;
条件控制语句;
练习:
package test;
public class test11 {
public static void main(String[] args) {
/*
需求:世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米)
假如我有一张足够大的纸,它的厚度是0.1毫米
请问,我折叠多少次,可以折成珠穆朗玛峰的高度?
*/
int count = 0;
double h = 0.1;
while (h < 8844430){
h *= 2;
count++;
}
System.out.println("需要折叠"+ count + "次");
}
}
package test;
import java.util.Scanner;
public class test12 {
public static void main(String[] args) {
/*
需求:给你一个整数 x。
如果x是一个回文整数,打印 true ,否则,返回 false。
解释:回文数是指正序(从左向右)和倒序(从右左)读都是一样的整数例如,121 是回文,而123 不是。
*/
Scanner sc = new Scanner(System.in);
System.out.println("输入一个整数x:");
int x = sc.nextInt();
int temp = x;
int num = 0;
while (x != 0){
int ge = x % 10;
x /= 10;
num = num * 10 + ge;
}
System.out.println(num);
System.out.println(temp==num);
}
}
public class test13 {
public static void main(String[] args) {
/*
需求:给定两个整数,被除数和除数(都是正数,且不超过int的范围)将两数相除
要求不使用乘法、除法和 % 运算符得到商和余数。
*/
int a = 25;
int b = 8;
int count = 0;
while (a >= b){
a -= b;
count ++;
}
System.out.println("商为"+ count);
System.out.println("余数为"+ a);
}
}
3.3 do…while
格式:
初始化语句;
do{
循环体语句;
条件控制语句;
}while(条件判断语句)