目录
一、概述
流程控制语句:Java提供了一些流程控制语句,来控制程序的执行流程
顺序结构:程序默认流程
分支结构:if、switch
循环结构:for、while、do...while
二、分支结构
1.if分支
定义:根据判定的结果(真或假)决定执行某个分支的代码
package com.pikaqiu.branch;
public class IfDemo1 {
public static void main(String[] args){
//格式1: if(条件表达式){语句体}
int heartBeat = 30;
if(heartBeat < 60 || heartBeat > 100){
System.out.println("心跳数据是:" + heartBeat + ",您可能需要进一步检查!");
}
System.out.println("检查结束");
System.out.println("----------------------------------");
//格式2: if(条件表达式){语句体} else{语句体}
double money = 5999;
if(money >= 1314){
System.out.println("当前红包发送成功");
} else{
System.out.println("余额不足,无法发送!");
}
System.out.println("----------------------------------");
//格式3: if(条件表达式){语句体} else if(条件表达式){语句体} ... else{语句体}
int score = 99;
if(score >= 0 && score < 60){
System.out.println("您的绩效为:C");
}else if(score >= 60 && score < 80){
System.out.println("您的绩效为:B");
}else if(score >= 80 && score < 90){
System.out.println("您的绩效为:A");
}else if(score >= 90 && score <= 100){
System.out.println("您的绩效为:A+");
}else{
System.out.println("录入的分数有问题!");
}
}
}
2.switch分支
定义:也是匹配条件去执行的分支,适合做值匹配的分支选择,结构清晰,格式良好
注意事项:1.表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String、不支持double、float、long;2.case给出的值不允许重复,且只能是字面量,不能是变量;3.不要忘记写break,否则会出现穿透现象
package com.pikaqiu.branch;
public class SwitchDemo3 {
public static void main(String[] args) {
//1.表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String、不支持double、float、long
//double a = 3.0; 错误,不支持double类型
// switch(a){
//
// }
//2.case给出的值不允许重复,且只能是字面量,不能是变量
int b = 31;
switch(3){
case 31 :
break;
// case b : //错误,不能是变量
// break;
case 3 :
break;
}
//3.不要忘记写break,否则会出现穿透现象
}
}
switch的穿透性:如果代码执行到没有写break的case块,执行完后将直接进入下一个case块执行代码(而且不会进行任何匹配),直到遇到break才跳出分支,这就是switch的穿透性
package com.pikaqiu.branch;
public class SwitchDemo4 {
public static void main(String[] args) {
int month = 4;
switch (month){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
System.out.println(month + "月是31天!");
break;
case 2 :
System.out.println(month + "月是闰年29天、非闰年28天!");
break;
case 4 :
case 6 :
case 9 :
case 11 :
System.out.println(month + "月是30天!");
break;
default:
System.out.println("数据有误");
}
}
}
三、循环结构
1.for循环
格式:for(初始化条件;循环条件;迭代语句){循环体语句(重复执行的代码);}
package com.pikaqiu.loop;
public class ForDemo1 {
public static void main(String[] args) {
for(int i = 0;i < 3;i++){
System.out.println("HelloWorld");
}
System.out.println("---------------------------");
for(int i = 0;i <= 5;i+=2){
System.out.println("HelloWorld");
}
}
}
案例:
package com.pikaqiu.loop;
public class ForDemo2 {
public static void main(String[] args) {
System.out.println("---------------案例1:1-5之和---------------");
int sum = 0;
for(int i = 1;i <= 5;i++){
sum += i;
}
System.out.println("1-5之和为:" + sum);
System.out.println("---------------案例2:求1-10的奇数和---------------");
// //方法1:
// int sum1 = 0;
// for(int i = 1;i <= 10;i++){
// if(i%2 == 1){
// sum1 += i;
// }
// }
// System.out.println("1-10的奇数和为:" + sum1);
int sum2 = 0;
for(int i = 1;i <= 10;i += 2){
sum2 += i;
}
System.out.println("1-10的奇数和为:" + sum2);
System.out.println("---------------案例3:求水仙花数---------------");
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 + "\t");
count++;
}
}
System.out.println(); //换行
System.out.println("水仙花数的个数为:" + count);
}
}
2.while循环
格式:while(循环条件){循环体语句;迭代语句;}
package com.pikaqiu.loop;
public class WhileDemo3 {
public static void main(String[] args) {
int i = 0;
while(i < 3){
System.out.println("HelloWorld");
i++;
}
}
}
案例:
package com.pikaqiu.loop;
public class WhileDemo4 {
public static void main(String[] args) {
//一般不知道循环次数时用while
//定义变量记录珠穆朗玛峰的高度和纸张的厚度
double peakHeight = 8848860;
double paperThickness = 0.1;
//定义一个变量记录纸张折叠的次数
int count = 0;
while(paperThickness < peakHeight){
paperThickness *= 2;
count++;
}
System.out.println("纸张此时的厚度为:" + paperThickness);
System.out.println("需要折叠的次数为:" + count);
System.out.println("---------------用for循环的方法---------------");
int count1 = 0;
double i = 0.1;
for(;i < 8848860;i*=2){
count1++;
}
System.out.println("纸张此时的厚度为:" + i);
System.out.println("需要折叠的次数为:" + count1);
}
}
3.do-while循环
格式:do{循环体语句;迭代语句;}while(循环条件);
package com.pikaqiu.loop;
public class DowhileDemo5 {
public static void main(String[] args) {
int i = 0;
do{
System.out.println("HelloWorld");
i++;
}while(i < 3);
}
}
三种循环的区别:1.for循环和while循环(先判断后执行);2.do...while(第一次先执行后判断)
for和while的区别:1.for循环和while循环的流程是一模一样的;2.如果已知循环次数建议使用for循环,如果不清楚循环多少次时建议使用while循环;3.for循环中,控制循环的变量只在循环中使用,而在while循环中,控制循环的变量可以在循环后继续使用
4.死循环
定义:一直循环的执行下去,如果没有干预不会停下来
package com.pikaqiu.loop;
import java.util.Scanner;
public class DeadForDemo6 {
public static void main(String[] args) {
// System.out.println("---------------for的死循环---------------");
// for( ; ; ){
// System.out.println("HelloWorld~~~");
// }
// System.out.println("---------------while的死循环---------------");
// //经典写法
// while(true){
// System.out.println("快乐的死循环~~~");
// }
// System.out.println("---------------do-while的死循环---------------");
// do{
// System.out.println("快乐的死循环~~~");
// }while(true);
System.out.println("---------------案例:密码验证---------------");
//需求:系统密码是520,请用户不断输入密码验证,验证不对输出密码错误,验证成功输出欢迎进入系统,并停止程序
//1.定义一个正确的密码
int okPassword = 520;
//2.定义一个死循环让用户不断的输入密码验证
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入正确的密码!");
int password = sc.nextInt();
if(password == okPassword){
System.out.println("登录成功~~~");
break; //结束当前循环
}else{
System.out.println("密码错误!");
}
}
}
}
5.循环嵌套
定义:循环中又包含循环
package com.pikaqiu.loop;
public class ForForDemo7 {
public static void main(String[] args) {
//场景:你犯错了,女朋友罚你说五天,每天三句我爱你
//嵌套循环的特点:外部循环每循环一次,内部循环全部执行完一次
for(int i = 0;i < 5;i++){
for(int j = 0;j < 3;j++){
System.out.println("I love you");
}
System.out.println("--------------------");
}
}
}
四、跳转关键字:break、continue
break: 跳出并结束当前所在循环的执行
continue: 用于跳出当前循环的当前执行,进入下一个循环
注意事项:1.break只能用于结束所在循环,或者结束所在switch分支的执行;2.continue只能在循环中进行使用
package com.pikaqiu.loop;
public class BreakAndContinueDemo8 {
public static void main(String[] args) {
//输出三次 快乐的洗碗~~~
for (int i = 0; i < 5; i++) {
System.out.println("快乐的洗碗~~~");
if(i == 2){
break; //跳出并结束当前循环的执行
}
}
//continue 跳出当前循环的当前执行,进入循环的下一次
//输出 洗碗:1 洗碗:2 洗碗:4 洗碗:5
for (int i = 1; i <=5 ; i++) {
if(i == 3){
continue; //立即跳出循环,进入循环的下一次
}
System.out.print("洗碗:" + i + " ");
}
}
}
五、案例技术:随机数Random类
1.Random的使用
Random生成随机数的特点:只能生成0—(n-1)直接的随机数
Random生成随机数的技巧:减加法
package com.pikaqiu.random;
import java.util.Random;
public class RandomDemo1 {
public static void main(String[] args) {
//1.导包
//2.创建随机数对象
Random r = new Random();
//3.调用nextInt功能(方法) 可以返回一个整型的随机数给你
for (int i = 0;i < 10;i++) {
int data = r.nextInt(10); //0-9 不包含10的随机数
System.out.println(data);
}
System.out.println("-------------------------");
for (int i = 0;i < 10;i++) {
//生成1-10的随机数 -1 --> 0-9 --> +1
int data1 = r.nextInt(10) + 1;
System.out.println(data1);
}
System.out.println("-------------------------");
//生成3-17的随机数 -3 --> 0-14 --> +3
int data2 = r.nextInt(15) + 3;
System.out.println(data2);
}
}
2.猜数字游戏
package com.pikaqiu.random;
import java.util.Random;
import java.util.Scanner;
public class RandomDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//获取一个1-100的随机数
Random r = new Random();
int rightNumber = r.nextInt(100) + 1;
//利用死循环让用户不断猜测,直到猜对
while(true){
System.out.println("请输入您猜测的数据(1-100)");
int number = sc.nextInt();
if(number == rightNumber){
System.out.println("猜对了!");
break; //跳出当前死循环
}else if(number > rightNumber){
System.out.println("猜大了!");
System.out.println("--------------------");
}else {
System.out.println("猜小了!");
System.out.println("--------------------");
}
}
}
}