流程控制语句
分类
1.顺序结构
2.分支结构/选择结构
3.循环结构
顺序结构
/*
顺序结构:
代码的在执行流程 从左到右 从上到下 依次执行
*/
public class Demo01 {
public static void main(String[] args) {
//从左到右
System.out.println(1+2+3+4+"HelloWorld"+5+6+7+8+9);
//从上到下
System.out.println("开始");
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");
System.out.println("E");
System.out.println("结束");
}
}
分支结构
格式
/*if语句格式1:
if(条件表达式){
语句体
}
执行流程:判断条件表达式的值 如果为true执行语句体,否则不执行
*/
public class Demo01If语句 {
public static void main(String[] args) {
int a = 10;
a = 11;
if (a>10){
System.out.println("a大于10");
}
}
}
/*
if语句格式2
if(条件表达式){
语句体1
} else{
语句体2
}
执行流程:判断条件表达式值 为true 执行语句体1 否则执行语句体2
*/
public class Demo02If语句格式2 {
public static void main(String[] args) {
int a= 10;
a = 11;
if (a>10){
System.out.println("a大于10");
}else {
System.out.println("a不大于10");
}
}
}
/*
if语句格式3
if(条件表达式1){
语句体1
}else if(条件表达式2){
语句体2
}else{
语句体3
}
执行流程:
1.判断条件表达式1 的值 为true 执行 语句体1 否则判断条件表达式2的值 如果为true 执行语句体2,否则执行语句体3
*/
public class Demo03If语句格式3 {
public static void main(String[] args) {
int a = 10;
a = 11;
if (a>10){
System.out.println("a大于10");
}else if (a<10){
System.out.println("a小于10");
}else {
System.out.println("a等于10");
}
}
}
分支语句switch
import java.util.Scanner;
/*
分支语句Switch:
格式:
switch(值){
case 值1:语句体1;break;
case 值2:语句体2;break;
... ...
default: 语句体N+1;
}
执行流程:
获取switch括号里面的值和case语句后面的值 进行匹配,如果匹配的上,就执行case语句后面的语句体
如果都不匹配,执行default语句
*/
/*
键盘录入一个星期数 判断是星期几
1.键盘录入
导包 创建对象 接收数据
2.使用switch语句 判断
*/
public class Demo04分支语句Switch {
public static void main(String[] args) {
//1.键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个星期数:");
int week = sc.nextInt();
//2.使用switch语句 判断
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("您输入的 数据有误!");
}
}
}
注意:switch 语言具有穿透性(case击穿)。如果语句中没有写break,
语句会一直向下执行。
/*
键盘录入一个月份判断是什么季节
12 1 2 冬季
3 4 5 春季
6 7 8 夏季
9 10 11秋季
switch语句的特点:
1.遇到break才停止执行。也叫case语句穿透性
*/
public class Demo05分支语句特点 {
public static void main(String[] args) {
//键盘录入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个月份:");
int month = scanner.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("您输入的月份有误!");
}
}
}
switch能使用的数据类型
分支语句Switch能够使用的数据类型:
required: ‘char, byte, short, int, Character, Byte, Short, Integer, String, or an enum’
循环语句
for循环
/*
for循环:
for(初始化语句;条件判断语句;条件控制语句){
循环体语句;
}
1.执行初始化语句
2.执行判断语句 ==> true 执行循环体语句 ==> 条件控制语句 ==> 条件判断语句
直到条件判断语句为false结束整个循环
*/
public class Demo01for循环 {
public static void main(String[] args) {
/*System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");*/
for (int i= 1;i<=5;i++){
System.out.println("HelloWorld");
}
//打印1-5之间整数
for (int i=1;i<=5;i++){
System.out.println(i);
}
//打印5--1
for(int i=5;i>=1;i--){
System.out.println(i);
}
}
}
while循环
/*
While循环
初始化语句
while(条件判断语句){
循环体语句
条件控制语句
}
1.初始化语句
2.执行条件判断语句 ==> true 执行循环体语句 ==> 条件控制语句 ==> 条件判断语句
直到条件判断语句结果为false结束整个while循环
*/
public class Demo03While循环 {
public static void main(String[] args) {
//打印1-5之间整数
for (int i=1;i<=5;i++){
System.out.println(i);
}
System.out.println("----------while----------");
int j = 1;
while (j<=5){
System.out.println(j);
j++;
}
System.out.println("----------for----------");
//打印5--1
for(int i=5;i>=1;i--){
System.out.println(i);
}
System.out.println("----------while----------");
int x = 5;
while (x>=1){
System.out.println(x);
x--;
}
System.out.println("----------求和----------");
//求1--100之间所有整数和使用while循环
int sum = 0;
int y = 1;
while (y<=100){
sum+=y;
y++;
}
System.out.println(sum);
}
}
do-while循环
/*
do-while循环
初始化语句
do{
循环体语句
条件控制语句
}while(条件判断语句);
执行流程:
1.初始化语句
2.循环体语句
3.条件控制语句
4.条件判断语句 ==> true 循环体语句 ==> 条件控制语句 ==> 条件判断语句
直到条件判断语句结果为false 结束循环
*/
public class Demo04DoWhile循环 {
public static void main(String[] args) {
//打印1-5之间整数
System.out.println("----------for----------");
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
System.out.println("----------while----------");
int j = 1;
while (j <= 5) {
System.out.println(j);
j++;
}
System.out.println("----------do-while----------");
int a = 1;
do{
System.out.println(a);
a++;
}while (a<=5);
System.out.println("----------for----------");
//打印5--1
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
System.out.println("----------while----------");
int x = 5;
while (x >= 1) {
System.out.println(x);
x--;
}
System.out.println("----------do-while----------");
int b = 5;
do {
System.out.println(b);
b--;
}while (b>=1);
}
}
三种循环区别
1.初始化变量的位置不同,for循环在循环内部,while和do-while在循环外
2.for循环结束后,初始的变量不能使用。while和do-while还可以使用初始变量
3.for循环和while循环先判断在执行,do-while先执行,再判断执行
练习
public class Demo07水仙花数格式化输出 {
public static void main(String[] args) {
//定义计数器
int count = 0;
//判断水仙花数 是就统计
for (int i = 100; i <= 999; i++) {
//求各个位上的数字
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.print(i + " ");
count++;
if (count % 2 == 0) {
System.out.println();
}
}
}
System.out.println("水仙花数有:" + count + "个");
}
}
/*
需求:世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米),假如我有一张足够大的纸,它的厚度是0.1毫米。
请问,我折叠多少次,可以折成珠穆朗玛峰的高度?
1.定义计数器
2.把珠峰的高度转成毫米
3.while(纸张厚度<珠峰高度){
纸张厚度 = 纸张的厚度 * 2
计数器加一
}
*/
public class Demo08珠穆朗玛峰 {
public static void main(String[] args) {
//定义计数器
int count = 0;
//8844.43米=8844430毫米
int zf = 8844430;
//纸张的厚度0.1
double paper = 0.1;
while (paper<zf){
// paper = paper *2;
paper *= 2;
count++;
}
System.out.println("折叠:"+count+"次");
}
}
死循环
三种
/*
死循环:
for(;;){}
while(true){}
do{}while(true)
*/
public class Demo10死循环 {
public static void main(String[] args) {
/*for (;;){
System.out.println("不要停...........");
}*/
/* while (true){
System.out.println("停不下了。。。。。");
}*/
do{
System.out.println("崩溃了...aaaaaaa...");
}while (true);
}
}
流程跳转控制语句
/*
break:结束离break最近的循环
continue:跳过本次循环继续下一次
*/
public class Demo11流程跳转控制语句 {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
if (i==2){
break;
}
System.out.println(i);
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
if (j == 3){
break;
}
System.out.println(i + " ===" + j);
}
}
System.out.println("--------------");
for (int i = 0; i < 5; i++) {
if (i==2){
continue;
}
System.out.println(i);
}
}
}
随机数
import java.util.Random;
/*
随机数类使用的步骤:
1.导包
import java.util.Random
2.创建对象
Random r = new Random();
3.生成随机数 数据
int i = r.nextInt(参数);
0 -- 参数减一之间数字
*/
public class Demo01 {
public static void main(String[] args) {
// 、、创建对象
Random r = new Random();
//生成随机数
int x = r.nextInt();
System.out.println(x); //int类型数据的取值范围内的数据
//生成0到参数之间随机数
for (int i=0;i<=100;i++) {
int num = r.nextInt(10); //0到9
System.out.println(num);
}
}
}
生成区间内随机数
import java.util.Random;
public class Demo02生成区间内的随机数 {
public static void main(String[] args) {
// 22 --- 99之间随机数 [22,99]
Random random = new Random();
//生成随机数
int num = random.nextInt(78)+22;//22--99
System.out.println(num);
/*
生成区间内随机数:
通用公式:random.nextInt(最大值-最小值+1)+最小值;
*/
// 33 -- 98 之间的随机数
int num2 = random.nextInt(66) + 33;
}
}
猜数字小游戏
import java.util.Random;
import java.util.Scanner;
/*
1--100之间的数字
猜数字小游戏
思路:
1.先随机生成一个1到100之间随机数
2.猜数字
键盘录入数字和随机数比较,如果大了 提示猜大了 如果小了 提示猜小了 如果相等 提示恭喜你猜对了,如果猜不对一直猜
*/
public class Demo03猜数字 {
public static void main(String[] args) {
//1.先随机生成一个1到100之间随机数
Random random = new Random();
int num = random.nextInt(100)+1;
System.out.println(num);
// 2.猜数字
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输你要猜的数字:");
int guessNum = scanner.nextInt();
if (guessNum>num){
System.out.println("您猜的数字大了!");
}else if (guessNum<num){
System.out.println("您猜的数字小了!");
}else {
System.out.println("恭喜猜对了!");
break;
}
}
}
}
/***
* .,, .,:;;iiiiiiiii;;:,,. .,,
* rGB##HS,.;iirrrrriiiiiiiiiirrrrri;,s&##MAS,
* r5s;:r3AH5iiiii;;;;;;;;;;;;;;;;iiirXHGSsiih1,
* .;i;;s91;;;;;;::::::::::::;;;;iS5;;;ii:
* :rsriii;;r::::::::::::::::::::::;;,;;iiirsi,
* .,iri;;::::;;;;;;::,,,,,,,,,,,,,..,,;;;;;;;;iiri,,.
* ,9BM&, .,:;;:,,,,,,,,,,,hXA8: ..,,,.
* ,;&@@#r:;;;;;::::,,. ,r,,,,,,,,,,iA@@@s,,:::;;;::,,. .;.
* :ih1iii;;;;;::::;;;;;;;:,,,,,,,,,,;i55r;;;;;;;;;iiirrrr,..
* .ir;;iiiiiiiiii;;;;::::::,,,,,,,:::::,,:;;;iiiiiiiiiiiiri
* iriiiiiiiiiiiiiiii;;;::::::::::::::::;;;iiiiiiiiiiiiiiiir;
* ,riii;;;;;;;;;;;;;:::::::::::::::::::::::;;;;;;;;;;;;;;iiir.
* iri;;;::::,,,,,,,,,,:::::::::::::::::::::::::,::,,::::;;iir:
* .rii;;::::,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,::::;;iri
* ,rii;;;::,,,,,,,,,,,,,:::::::::::,:::::,,,,,,,,,,,,,:::;;;iir.
* ,rii;;i::,,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,,::i;;iir.
* ,rii;;r::,,,,,,,,,,,,,:,:::::,:,:::::::,,,,,,,,,,,,,::;r;;iir.
* .rii;;rr,:,,,,,,,,,,,,,,:::::::::::::::,,,,,,,,,,,,,:,si;;iri
* ;rii;:1i,,,,,,,,,,,,,,,,,,:::::::::,,,,,,,,,,,,,,,:,ss:;iir:
* .rii;;;5r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,sh:;;iri
* ;rii;:;51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.:hh:;;iir,
* irii;::hSr,.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,sSs:;;iir:
* irii;;:iSSs:.,,,,,,,,,,,,,,,,,,,,,,,,,,,..:135;:;;iir:
* ;rii;;:,r535r:...,,,,,,,,,,,,,,,,,,..,;sS35i,;;iirr:
* :rrii;;:,;1S3Shs;:,............,:is533Ss:,;;;iiri,
* .;rrii;;;:,;rhS393S55hh11hh5S3393Shr:,:;;;iirr:
* .;rriii;;;::,:;is1h555555h1si;:,::;;;iirri:.
* .:irrrii;;;;;:::,,,,,,,,:::;;;;iiirrr;,
* .:irrrriiiiii;;;;;;;;iiiiiirrrr;,.
* .,:;iirrrrrrrrrrrrrrrrri;:.
* ..,:::;;;;:::,,.
*/