导言:
流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块。
其流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:顺序结构、分支结构和循环结构,如下图所示:
顺序结构:
从上到下,从左到右
分支结构:
单分支:
if(布尔表达式){
代码块...
}
双分支:
if(布尔表达式){
代码块1...
}else{
代码块2...
}
如果布尔表达式的值为true,执行代码块1,如果为false,执行代码块2
多分支:
if(布尔表达式1){
代码块1...
}else if(布尔表达式2){
代码块2...
}else if(布尔表达式3){
代码块3...
}else{
}
多选一
如果表达1的结果为true,只执行代码块1,执行完之后,分支语句结束
如果表达1为false,则考虑表达式2,如果2成立,则执行2,后续的不考虑
switch 语句:
switch(表达式或者值){
case 1:break;
case 2: break;
case 3: break;
default: break;
}
1.switch语句会根据表达式的值从相匹配的执行,一直执行到break标签处开始ak语句处或者是switch语句的末尾。与任一case值不匹配,则进入default语句(如果有的话)
2.只能处理等值条件判断的情况,且表达式必须为byte,short,int或char类型 ,String和枚举类型,不能是double,float,boolean
1.5开始,可以为Enum,枚举
1.7开始,可以为String
3.常量值必须是与表达式类型兼容的特定的一个常量
4.不允许有重复的case值
5.default子句为可选
if多分支和switch case语句的选择:
多分支跟switch多分支表示范围比较合适
switch表示具体的值比较合适
例题:
1.掷骰子
1~3 小
4~6 大
1、随机生成一个数值(1~6)
Random
2、提示猜大小 1.大 2.小
3、判断结果是否中出
import java.util.Random;
import java.util.Scanner;
public class Test5{
public static void main(String[]args){
//1.创建一个随机数生成器
Random rd = new Random();
//2.使用生成器生成一个1~6之间的随机数
int num = rd.nextInt(6)+1;//rd.nextInt(6):生成数的范围0~5
//3.猜测
System.out.println("请猜测:1.大 2.小");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
//5.打印结果
/* if(num > 3){
System.out.println("开出结果为大");
}
if(num < 4){
System.out.println("开出结果为小");
} */
if(num > 3){
if(choice == 1){
System.out.println("恭喜中出");
}else{
System.out.println("很遗憾~");
}
System.out.println("开出结果为大:"+num);
}else{
if(choice == 2){
System.out.println("恭喜中出");
}else{
System.out.println("很遗憾~");
}
System.out.println("开出结果为小:"+num);
}
}
}
2.三个骰子,大、中、小
大:13-18
中:7-12
小:1-6
总点数的变量 count,count1,count2,count3
随机生成count1,count2,count3(1-6)
使用多分进行判断,打印结果
import java.lang.Math;
public class Test6{
public static void main(String[]args){
int count = 0;
int count1,count2,count3;
//使用数学类中的方法,生成随机值
count1 =(int)(Math.random() * 6) + 1;//Math.random()值 0~0.99999...,
count2 =(int)(Math.random() * 6 + 1);
count3 =(int)(Math.random() * 6 + 1);
count = count1 + count2 + count3;
if(count >= 13 && count <= 18){
System.out.println("结果为大:三个骰子点数分别为:"+count1+","+count2+","+count3);
}else if(count >= 7 && count <= 12){
System.out.println("结果为中:三个骰子点数分别为:"+count1+","+count2+","+count3);
}else{
System.out.println("结果为小:三个骰子点数分别为:"+count1+","+count2+","+count3);
}
}
}
3.输入一个数字,给出对应季节的提示
春 3,4,5
夏 6,7,8
秋 9,10,11
冬 12,1,2
例如:输入数字3,打印 春意盎然,春暖花开
输入:12 打印 冬雪皑皑
1.扫描器
2.输入,记录输入的值
3.根据输入的值进行多分支判断
4.打印对应结果
import java.util.Scanner;
public class Test7{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入任意一个月份");
int month = sc.nextInt();
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 == 1 || month == 2 || month == 12){
System.out.println("冬雪皑皑");
}else{
System.out.println("输入月份有误");
}
char ch = 'A';
switch(ch){
case 'A':
case 'B':
}
}
}
用Switch case 语句重写:
import java.util.Scanner;
public class Test8{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入任意一个月份");
int month = sc.nextInt();
switch(month){
case 12:
case 1:
case 2:
System.out.println("冬雪皑皑");
break;
case 3:
case 4:
case 5:
System.out.println("春意盎然");
break;
case 6:
case 7:
case 8:
System.out.println("夏日炎炎");
break;
case 9:
case 10:
case 11:
System.out.println("秋风瑟瑟");
break;
default:
System.out.println("月份有误");
break;
}
}
}
4.键盘输入月份,年份 显示对应的天数 switch 去做
判断年份 是闰年还是平年
①能被4整除不能被100整除
②能被400整除
import java.util.Scanner;
class YearTest1{
public static void main(String [] args){
//创建Scanner对象
Scanner input = new Scanner(System.in);
//准备数据
System.out.println("请输入一个年份");
int year = input.nextInt();
System.out.println("请输入一个月份");
int month = input.nextInt();
//定义变量记录每个月的天数
int days = 0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
//System.out.println(month +"月 有31天");
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
//System.out.println(month+"月,有 30 天");
days = 30;
break;
case 2:
if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
//System.out.println(month+"月,有 29 天");
days = 29;
}else{
//System.out.println(month+"月,有 28 天");
days = 28;
}
break;
}
System.out.println(year+"年,"+month+" 月,有 "+days+"天");
}
}
循环结构:
循环结构都由如下四个结构组成:
初始化条件、 只执行一次
循环条件、 循环最后执行的条件
循环体、
迭代条件
while循环
do-while循环
for循环
While循环
初始化条件;
while(条件判断){
循环体;
迭代条件;
}
在循环刚开始时,会计算一次“布尔表达式”的值,若条件为真,执行循环体。而对于后来每一次额外的循环,都会在开始前重新计算一次。
语句中应有使循环趋向于结束的语句,否则会出现无限循环–––”死”循环。
while(true){
循环体;
迭代条件;
} 死循环
例题:
1.输出10次HelloWorld
class LoopTest1{
public static void main(String [] args){
int i = 1;
//循环条件i <= 10
while(i <= 5){
//循环体
System.out.println("Hello World\t"+i);
//迭代条件
//i++;
++i;
}
System.out.println("Over");
}
}
2.求1~100内偶数的和
①输出1~100内的100个数
②拿到100内的偶数
偶数:能被2整除的数
③ 求和
定义变量记录总和
class LoopTest2{
public static void main(String [] args){
//初始化条件
int i = 1;
//定义变量记录总和
int sum = 0;
//循环条件
while( i <= 100){
//循环体 能被2整除的数
if(i % 2 == 0){
System.out.println(i);
//sum = sum + i;
sum += i;
}
//迭代条件
i++;
}
System.out.println("总和是 "+sum);
System.out.println("Over");
}
}
3.求100内能被5整除的数 每行输出5个 的 整体的和 及其个数
①列出1~100内的数
②列出1~100内能被5整除的数
③定义变量记录满足条件的元素的数量
int count = 0;
④定义变量记录总和
int sum = 0;
class LoopTest3{
public static void main(String [] args){
int i = 1;
//定义变量记录数量
int count = 0;
//定义变量记录总和
int sum = 0;
while(i<=100){
// 循环条件 判断是否能被5整除
if( i % 5 == 0){
//每次给本身+1
//count = count + 1;
count++;
System.out.print(i+"\t");
//每行输出5个元素
if(count % 5 == 0){
System.out.println();
}
//记录总和
//sum = sum + i;
sum += i;
}
//迭代条件
i++;
}
System.out.println("1~100内偶数的和是\t"+sum+",一共有 "+count+" 个");
}
}
4.键盘录入正数 或者 负数的整数
当录入0时终止程序 输出刚才一共录入了多少个正数 和负数
1 2 3
-1 -3
0
正数的个数有 3
负数的个数有 2
import java.util.Scanner;
class LoopTest4{
public static void main(String [] args){
//创建Scanner对象
Scanner input = new Scanner(System.in);
//定义变量统计正数的个数
int zhengShu = 0;
//定义变量统计负数的个数
int fuShu = 0;
//定义初始化条件
int num = 90;
while(num !=0 ){// ==
System.out.println("请您输入一个数");
num = input.nextInt();
if(num > 0){
zhengShu++;
}
if(num < 0){
fuShu++;
}
}
System.out.println("正数的个数是\t"+zhengShu+"\n负数的个数是\t"+fuShu);
}
}
do while循环
初始化条件
do{
循环题;
迭代条件
}while(循环条件);
例题:
1.输出100内偶数的和
1.输出1~100内的数
class DoWhileTest{
public static void main(String [] args){
//初始化条件
int i = 1;
//定义变量接收数据的和
int sum = 0;
do{
//循环体
if(i%2==0){
System.out.println(i);
sum+=i;
}
//迭代条件
i++;
//循环条件
}while(i<=100);
System.out.println("偶数的和是\t"+sum);
System.out.println("Over");
}
}
区别
while:判断初始化条件是否满足循环条件 不满足不执行循环体
dowhile:即使初始化条件不满足循环条件也会执行一次循环体
For循环
for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构
语法形式
for (初始表达式;循环条件;迭代条件) {
循环体;
}
增强for循环(foreach循环):
for(type x : collection){
}
注意事项:
1.for循环在执行条件测试后,先执行程序部分,再执行步进。
2.在for语句的初始化部分声明的变量,其作用域为整个for循环体
3.“初始化”和“循环条件表达式”部分可以使用逗号来执行多个操作
4.如果三个部分都为空语句(分号不能省),相当于一个无限循环
5.初始化条件可以放到循环的外面作用域(作用范围)变大
6.缺少循环条件会变为死循环
7.缺少迭代条件会变为死循环
8.缺失所有的条件变为死循环
9.dos窗口终止死循环 ctrl + c
例题
1.1~100内能被3整除的数 每行输出 6个 求个数 求总和
class ForTest1{
public static void main(String [] args){
for(int i = 1;i<=0;i++){
System.out.println("HelloWorld");
}
System.out.println("Over");
System.out.println("-------------------------------------");
//记录数量
int count = 0;
//定义变量求和
int sum = 0;
for(int i = 1;i<=100;i++){
//判断是否能被3整除
if(i % 3 ==0){
System.out.print(i+"\t");
count++;
/*if(count % 6 ==0){
System.out.println();
}*/
//求和
sum += i;
}
if( i % (3*6) == 0 ){
System.out.println();
}
}
System.out.println("\n总和是\t"+sum+",个数是 "+count);
}
}
2.输出200以内的整数,被3整除后面输出 foo,
被5整除输出 biz,被7整除输出baz,同时被3,5整除输出foo biz ,同时被3,7整除输出foo baz,同时被3,5,7整除输出foo biz baz。
class ForTest4{
public static void main(String [] args){
System.out.println(i);
for(int i = 1;i<=200;i++){
//刷新str的值
String str = "";
if(i % 3 == 0){
str = "\tfoo";
}
if(i % 5 == 0){
str += "\tbiz";
}
if(i % 7 == 0){
str += "\tbaz";
}
System.out.println(i+ str);
}
}
}
for循环与while,dowhile循环区别:
当循环次数不固定时 优先使用while循环
当循环次数固定时 使用for循环
无论如何都想执行一次循环体 do{}while();当初始化条件不满足循环条件也会执行一次循环体
特殊的流程控制语句
break:结束循环
只能用在循环和switch中
continue:结束本次(当前)循环继续下一次循环
只能用在循环中
return;结束方法
注意:
错误: 无法访问的语句
在特殊的流程控制语句下不可以存在其他的代码
例子:
class BreakTest{
public static void main(String [] args){
for(int i = 0;i<10;i++){
if(i == 5){
//continue;
return;
System.out.println("如果此句可以输出 给大家一百万");
}
System.out.println(i);
}
System.out.println("Game Over!!");
}
}
多重循环
三种循环方式
while
do-while
for
多重循环(循环嵌套)
一个循环体内又包含另一个完整的循环结构
任何两种循环都可以相互嵌套
可以任意层次循环,但是一般不超过3层
多重循环执行过程
外层循环变量变化一次,内层循环变量要变化一遍
for(循环条件1){
//循环操作1
for(循环条件2){
//循环操作2
}
}
while(循环条件1){
//循环操作1
for(循环条件2){
//循环操作2
}
}
例题:
1.打印9*9乘法表,使用for嵌套
public class DoubleLoopTest{
public static void main(String[] args){
for(int i=1; i<=9;i++){
for(int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println();
}
}
}
2.打印
外层循环控制行数内层循环控制列数
行数和星星的关系
h s
* = 1 1
** = 2 2
*** = 3 3
**** = 4 4
***** =5 5
class DoubleLoopTest2{
public static void main(String [] args){
//外层控制行数
for(int i = 1;i<=5;i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
3.打印
h k s
—-******* 1 4 7
—******* 2 3 7
–******* 3 2 7
-******* 4 1 7
******* 5 0 7
k = 5 - h;
行数,空格,和星星的关系如上
class DoubleLoopTest3{
public static void main(String [] args){
//确定外层循环
for(int h = 1;h<=5;h++){
//输出空格
for(int k = 1;k <= 5-h;k++){
System.out.print(" ");
}
//输出星星
for(int s = 1;s<=7;s++){
System.out.print("*");
}
//换行
System.out.println();
}
}
}
4.打印
—-*
—* *
–* * *
-* * * *
* * * * *
-* * * *
– * * *
— * *
—- *
分析星星,空格,和行数的关系
—-* 1 1 4 k = 5 - h;
—* * 2 2 3 s = h;
–* * * 3 3 2
-* * * * 4 4 1
* * * * * 5 5 0
-* * * * 1 4 1
– * * * 2
— * *
—- *
class DoubleLoopTest4{
public static void main(String [] args){
//确定外层循环5次
for(int h = 1;h<=5;h++){
//输出空格
for(int k = 1; k <= 5 - h;k++){
System.out.print(" ");
}
//输出星星
for(int s = 1;s <= h; s++){
System.out.print("* ");
}
//换行
System.out.println();
}
//菱形的下半部分
for(int h = 1; h<=4;h++){
//输出空格
for(int k = 1;k<=h;k++){
System.out.print(" ");
}
//输出星星
for(int s = 1;s <= 5 - h;s++){
System.out.print("* ");
}
//换行
System.out.println();
}
}
}
5.将使用循环分别实现将101 进值正整数变成 二进制数
写法1:
将10进制的正整数转为二进制
分析:
1.不停的对被除数取模取商此处为重复操作 考虑使用 循环
2.不同的10进制数转为二进制数 循环的次数不固定 所以考虑使用while循环
3.第一次对10除以2 拿到商为5 余数是 0
第二次对5除以2 拿到商为2 余数是1
第三次对2除以2 拿到商为1 余数是0
第四次对1除以2 拿到商为0 余数是1
总结:上一次结果的商作为下一次运算的被除数
class HomeTest1{
public static void main(String [] args){
//定义一个数
int num = 10;
//定义字符串接收余数
String str = "";
while(num!=0){
//求商
int shang = num / 2;//2
//求余数
int yuShu = num % 2;//1
System.out.println(yuShu);//0 1
str = yuShu+ str;//10
System.out.println("--->"+str);
//将商变为被除数
num = shang;//5
}
}
}
写法2:
用StringBuffer写:
public class Scale{
public static void main(String[] args){
StringBuffer buffer=new StringBuffer();
int a=101;
int b=0;
while(a>0){
b=a%2;
buffer.append(b);
a/=2;
}
buffer.reverse();
System.out.println(buffer);
}
}
多重循环中使用特殊语句和lable标签的使用
break, continue,return
嵌套循环下特殊的流程控制语句执行过程
break:结束最近的循环
直接使用break结束外层循环:
方式1:在外层循环内定义boolean类型变量 通过变量传递的方式结束
方式2:定义循环lable(标签 符合命名规范和命名规则即可)
break结束的是 label 所在层的循环
标签:
label:外部循环{
内部循环{
//…
break;
//…
continue;
//…
continue label1;
//…
break label1;
}
}
“标签”是指后面跟一个冒号的标识符,例如:label:
对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:
我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,
它们就会中断到存在标签的地方。
在Java里唯一需要用到标签的地方就是拥有嵌套循环,而且想中断或继续多个嵌套级别的时候。
例题:
打印101-150之间所有的质数
public class PrimeNumber {
public static void main(String args[]) {
int count = 0;
outer: for (int i = 101; i < 150; i ++) {
for (int j = 2; j < i / 2; j++) {
if (i % j == 0)
continue outer;
}
System.out.print(i+ " ");
}
}
}
例题:
class BreakDoubleLoop{
public static void main(String [] args){
/*
for(int i = 1;i<=5;i++){
boolean flag = false;
for(int j = 1;j <=15;j++){
if(j==10){
flag = true;
break;
}
System.out.print(j+"\t");
}
if(flag){
break;
}
System.out.println();
}
*/ 用lable
l:for(int i = 1;i<=5;i++){
for(int j = 1;j <=15;j++){
if(j==10){
break l;
}
System.out.print(j+"\t");
}
System.out.println();
}
System.out.println("Game Over!!!");
}
}
continue:结束当前循环继续下一次循环
输出5行1234678910
class ContinueDoubleLoop{
public static void main(String [] args){
for(int i = 1;i <= 5 ;i++){
lable:for(int j = 1; j<= 10 ;j++){
if( j == 5){
//continue lable;
return;
}
System.out.print(j+"\t");
}
System.out.println();
}
System.out.println("Game Over");
}
}
例题:
1.输出2~100内的质数
质数:1和本身的两个约数
class ZhiShuTest{
public static void main(String [] args){
for(int i = 2;i<=100;i++){
boolean flag = true;
for(int j = 2;j < i;j++){
//不是质数
if(i % j == 0){
flag = false;
}
}
//判断当是质数时输出
//if(flag==true){
if(flag){
System.out.println(i);
}
}
}
}
二分法求2-100以内的质数:
class ZhiShuTest1{
public static void main(String [] args){
l:for(int i = 2;i<=100;i++){
for(int j = 2;j <= i/2;j++){
//不是质数
if(i % j == 0){
continue l;
}
}
System.out.println(i);
}
}
}
博弈小游戏 :
博弈游戏:三个骰子,约定三个骰子的点数
最大点数:18
最小点数:3
要求:
1、大于9小于等于18是大,否则是小。
2、初始钱是5000;
3、每次只能下注50的倍数但最大不超过1000
4、最后一次下注必须要大于等于50
5、当下注金额小于余额时提示充值,如果不充值直接退出程序。
n:较大的数
m:较小的数
Math.random()*(n-m+1)+m;
(int)(Math.random()*(89-22+1)+m);
1.搭建框架
主程序包含在循环内
2.创建选择菜单
1.进入游戏
1.用户押大小
2.用户下注
1.下注的金额是否大于本金
1.不足
充值 重新下注 退出
2.满足
能被50整除 小于1000
3.电脑生成随机数
4.判断电脑点数的大小
5.用户大小与电脑大小进行对比产生结果
2.退出游戏
*/
import java.util.*;
public class BoYiGame{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int choose=0; //选择
int dian=0; //庄家点
int daXiao=0; //庄家的大小
int selfDaXiao=0; //自己猜的大小
int money1=1000; //初始的1000;
int paymoney=0; //押注的钱
int money2=0; //充值的钱
l: while(true){
System.out.println("============欢迎来到博弈游戏=============");
System.out.println("============1、进入游戏 2、退出游戏=== 请选择:");
choose=sc.nextInt();
if(choose==1){
System.out.println("进入游戏成功!");
dian=(int)(Math.random()*6+1)*3;
if(dian>9){
daXiao=1;
}
else{
daXiao=2;
}
System.out.println("庄家点数已生成,请下注:1、大 2、小");
selfDaXiao=sc.nextInt();
System.out.println("买定离手,请压钱:");
paymoney=sc.nextInt();
while(true){
if(paymoney>money1){
System.out.println("操作不成功,余额不足,请及时充值,现在可以1、前往充值中心充值 2 修改下注金额 3 结束游戏");
choose=sc.nextInt();
if(choose==1){
System.out.println("欢迎来到支付宝充值中心,请输入想要充值的金额");
money2=sc.nextInt();
System.out.println("充值成功,您的余额为:"+(money1+money2));
System.out.println("请您重新下注:");
paymoney=sc.nextInt();
}
else if(choose==2){
System.out.println("请您重新下注:");
paymoney=sc.nextInt();
}
else{
System.out.println("没钱回家!");
break l;
}
}
else{
if(paymoney%50==0&&paymoney<=1000&&paymoney>0){
break;
}
else{
System.out.println("押注必须小于1000且是50的倍数,请重新下注");
paymoney=sc.nextInt();
}
}}
if(selfDaXiao==daXiao){
money1+=paymoney;
System.out.println("恭喜您猜对了");
System.out.println("您的余额为:"+money1);
}
else{
money1-=paymoney;
System.out.println("菜的抠脚!");
System.out.println("您的余额为:"+money1);
}
}
else if(choose==2){
System.out.println("退出游戏成功!");
break;
}
else{
System.out.println("输入有误,请重新输入!");
}
}
}
}
小结:
选择结构
if语句 单、双、多分支选择结构,等值、不等值判断均可
switch语句 只有多分支选择结构 只针对等值判断
循环结构
while循环 先判断再循环 适合循环次数不固定情况
do-while循环 先循环再判断 适合循环次数不固定情况
for循环 适合循环次数固定情况
循环跳转语句
break 跳出本层循环,跳出外层循环需要结合标签或符号位实现
continue 提前结束本次循环
return 结束当前方法
多重循环
任何两种循环都可以相互嵌套
外层循环变量变化一次,内层循环变量要变化一遍
求质数的4种写法:
一、if语句控制
public class PrimeNumber{
public static void main(String[] args){
for(int i=2;i<=100;i++){
for(int j=2;j<i;j++){
if(i%j==0){
System.out.println(i+"不是质数");
break;
}
else {
System.out.println(i+"是质数");break;
}
}
}
}
}
二、布尔类型控制
class ZhiShuTest{
public static void main(String [] args){
for(int i = 2;i<=100;i++){
boolean flag = true;
for(int j = 2;j < i;j++){
//不是质数
if(i % j == 0){
flag = false;
}
}
//判断当是质数时输出
//if(flag==true){
if(flag){
System.out.println(i);
}
}
}
}
三、continue和标签的使用
class ZhiShuTest1{
public static void main(String [] args){
l:for(int i = 2;i<=100;i++){
for(int j = 2;j <= i/2;j++){
//不是质数
if(i % j == 0){
continue l;
}
}
System.out.println(i);
}
}
}
四、判断条件的开方,效率较高
class ZhiShuTest1{
public static void main(String [] args){
l:for(int i = 2;i<=100;i++){
for(int j = 2;j <= Math.sqrt(i);j++){
//不是质数
if(i % j == 0){
continue l;
}
}
System.out.println(i);
}
}
}
其中三和四种方法,i/2,和 Math.sqrt(i)效果相同。
本文详细介绍了程序设计中的流程控制语句,包括条件分支结构(if、switch)、循环结构(while、do-while、for)及相关的特殊流程控制语句(break、continue)。通过丰富的示例代码展示了不同控制语句的应用场景。
1959

被折叠的 条评论
为什么被折叠?



