day03
-
流程控制语句
-
分类 (了解)
-
顺序结构
-
选择结构/分支结构
-
循环结构
-
-
顺序结构
-
执行流程:从上到下 从左到右
-
-
选择结构/分支结构
-
if语句
格式1:
if(条件表达式){语句体}
执行流程 :先判断条件表达式的值,如果为true执行 语句体 否则不执行
格式2:
if(条件表达式){语句体1}else{语句体2}
执行流程::先判断条件表达式的值,如果为true执行 语句体1 否则执行语句体2
格式3:
if(条件表达式1){语句体1}else if(条件表达式2){语句体2}else{语句体3}
执行流程: 先判断条件表达式1的值,如果为true执行 语句体1 否则执行判断条件表达式2 如果为true执行 语句体2 否则执行语句体3
-
switch
格式:
switch(值){
case 值:
语句体1; break;
...............default:
语句体 n+1;
}执行流程:获取switch后面小括号的值和case语句后面的值匹配,如果匹配上,执行后面语句体,如果都不匹配执行default后面的语句体n+1
-
-
-
-
循环结构
-
for循环
格式:for(初始化语句;条件判断语句;条件控制语句){
循环体语句;
}
执行流程:1.先执行初始化语句 2. 执行条件判断语句 ==> true 执行循环体语句 ==> 条件控制语句 ==> 条件判断语句 直到条件判断语句结果为 false停止循
-
while循环
格式:
初始化语句
while(条件判断语句){
循环体语句;
条件控制语句
}
执行流程:1.先执行初始化语句 2. 执行条件判断语句 ==> true 执行循环体语句 ==> 条件控制语句 ==> 条件判断语句 直到条件判断语句结果为 false停止循环
-
do-while
格式:
初始化语句
do{
循环体语句;
条件控制语句
}while(条件判断语句);
执行流程:1.先执行初始化语句 2.执行循环体语句 3. 条件控制语句 4. 执行条件判断语句==>true 执行循环体语句 ==> 条件控制语句 ==> 条件判断语句 直到条件判断语句结果为false停止循环
-
-
死循环
-
for循环死循环
for(;;){语句体}
-
while死循环
while(true){语句体}
-
do-while死循环
do{语句体}while(true)
-
-
三种循环的去区别
-
1.for循环和while do-while 初始化语句 for循环的初始化语句在循环内部出了循环就不能使用,while和do-while在循环的外面,循环结束还可以使用
-
2.for循环和while循环 先判断后执行,do-while先执行一次在判断执行
-
-
流程跳转控制语句
-
break
-
continue
-
-
随机数
-
导包
-
import java.util.Random;
-
-
创建对象
-
Random r = new Random();
-
-
生成随机数
-
int i = r.nextInt();
-
-
作业 22 -- 99
-
1.流程控制语句
1.1 分类
1.顺序结构
2.分支结构/选择结构
3.循环结构
1.2 顺序结构
/*
顺序结构:
代码的在执行流程 从左到右 从上到下 依次执行
*/
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("结束");
}
}
1.3 分支结构 if
/*
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");
}
}
}
1.4 分支语句 switch
1.4.1 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("您输入的 数据有误!");
}
}
}
1.4.2 switch特点case语句具有穿透性
/*
键盘录入一个月份判断是什么季节
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("您输入的月份有误!");
}
}
}
public class Demo06分支语句case语句特点 {
public static void main(String[] args) {
//键盘录入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个月份:");
int month = scanner.nextInt();
//判断季节
switch (month) {
case 12, 1, 2:
System.out.println("冬季");
break;
case 3, 4, 5:
System.out.println("春季");
break;
case 6, 7, 8:
System.out.println("夏季");
break;
case 9, 10, 11:
System.out.println("秋季");
break;
default:
System.out.println("您输入的月份有误!");
}
}
}
1.4.3 switch 能够使用的数据类型
/*
分支语句Switch能够使用的数据类型:
required: 'char, byte, short, int, Character, Byte, Short, Integer, String, or an enum'
*/
public class Demo07分支语句Switch能够使用的数据类型 {
public static void main(String[] args) {
int a = 10;
switch (a){
}
byte b = 20;
switch (b){
}
short c = 30;
switch (c){
}
char d = 'a';
switch (d){
}
/*long e = 20L;
switch (e){
}*/
/*float f = 3.14F;
switch (f){
}*/
/*double g = 3.14;
switch (g){
}*/
/*boolean flag = true;
switch (flag){
}*/
String str = "abc";
switch (str){
}
WEEK week = WEEK.MONDAY;
switch (week){
}
}
}
enum WEEK{
MONDAY,SUNDAY
}
1.5练习
public class Demo08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int i = scanner.nextInt();
if (i%2==0){
System.out.println(i+"是偶数");
}else {
System.out.println(i+"是奇数");
}
}
}
1.6 循环结构
1.6.1 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);
}
}
}
1.6.2 for练习求和
/*
求1--100之间的整数和
求1--100之间的奇数和
求1--100之间的偶数和
*/
public class Demo02for练习 {
public static void main(String[] args) {
//求1--5之间的整数和
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
System.out.println(sum);
//求1--100之间整数和
int sum2 = 0;
for (int i = 1; i <= 100; i++) {
sum2 += i;
}
System.out.println(sum2);
//求1--100之间的奇数和
int sum3 = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
sum3 += i;
}
}
System.out.println(sum3);
//求1--100之间的偶数和
int sum4 = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum4 += i;
}
}
System.out.println(sum4);
}
}
1.6.3 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);
}
}
1.6.4 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.6.5 三种循环区别
/*
三种循环区别:
1.初始化变量的位置不同 for循环在循环内部 while和do-while在循环外面
for循环结束后 初始化的变量不能使用
while和do-while还可以使用初始变量值
2.for循环和while循环先判断再执行
do-while先执行一次 在判断执行
*/
public class Demo05三种循环区别 {
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(i); //初始化值在循环体内部
System.out.println("----------while----------");
int j = 1;
while (j <= 5) {
System.out.println(j);
j++;
}
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("a = " + a);
}
}
1.6.6 练习 水仙花数
/*
求水仙花数
1.水仙花数是3位数 100 --- 999
2.求这个三位数 各个位上数数字
3.求各个位上数字的立方和
4.如果满足水仙花数的条件 这个三位数和各个位上数字的立方相等 就是水仙花数
*/
public class Demo06求水仙花数 {
public static void main(String[] args) {
for (int i = 100; i <= 999; i++) {
//求各个位上的数字
int ge = i%10;
int shi = i/10%10;
int bai = i/100;
//求各个位上数字的立方和
int sum = ge*ge*ge + shi*shi*shi + bai*bai*bai;
//判断
if (i == sum){
System.out.println(i);
}
}
}
}
1.6.7 练习 水仙花数格式化输出
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 + "个");
}
}
1.6.8 练习 珠峰
/*
需求:世界最高山峰是珠穆朗玛峰(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+"次");
}
}
1.6.9 练习 减肥计划
import java.util.Scanner;
/*
需求:键盘录入星期数,显示今天的减肥活动。
周一:跑步
周二:游泳
周三:慢走
周四:动感单车
周五:拳击
周六:爬山
周日:好好吃一顿
*/
public class Demo09减肥计划 {
public static void main(String[] args) {
//键盘录入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入星期数:");
int week = scanner.nextInt();
//减肥计划
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("您输入的星期数有误!");
}
}
}
1.6.10 死循环
/*
死循环:
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);
}
}
1.6.11 流程跳转控制语句
/*
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);
}
}
}
1.7 随机数
1.7.1随机数入门
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);
}
}
}
1.7.2 生成区间内的随机数
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;
}
}
1.7.3 猜数字小游戏
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;
}
}
}
}