文章目录
流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑块。
程序设计中规定的三种流程控制结构,即:
-
顺序结构:
- 程序从上到下逐行地执行,中间没有任何判断和跳转。
-
分支结构:
- 根据条件,选择性的执行某段代码。
- 有
if…else
和switch-case
两种分支语句。
-
循环结构:
- 根据循环条件,重复性的执行某段代码。
- 有
for
、while
、do-while
三种循环语句 - JDK5.0提供了foreach循环,方便的遍历集合、数组元素。
目录:
分支语句(if…else)条件判断结构
结构1:单分支条件判断:if
if(条件表达式){
语句块;
}
结构2:双分支条件判断:if…else
if(条件表达式1){
语句块1;
} else if(条件表达式2){
语句块2;
} else {
语句块3;
}
案例1:心率范围60-100,超过范围提示用户需要做进一步检查
int heartRate = 80;
if(heartRate >= 60 && heartRate <=100){
System.out.println("心率正常");
} else {
System.out.println("心率不正常,请做进一步检查");
}
案例2:定义一个整数,判定是偶数还是奇数
int num = 13;
if(num % 2 == 0){
System.out.println(num + "是偶数");
} else {
System.out.println(num + "是奇数");
}
案例3:岳小鹏参加Java考试,他和父亲岳不群达成承诺:如果:成绩为100分时,奖励一辆跑车:成绩为[80,99]时,奖励一辆山地自行车:当成绩为[60,79]时,奖励环球影城一日游:其它时,胖揍一顿。说明: 默认成绩是在[0,100]范围内
int score = 60;
if(score == 100){
System.out.println("奖励一辆跑车");
} else if(score >= 80 && score <= 99){
System.out.println("奖励一辆山地自行车");
} else if(score >= 60 && score <= 79){
System.out.println("奖励环球影城一日游");
} else {
System.out.println("胖揍一顿");
}
Scanner:键盘输入功能的实现
- 如何从键盘获取不同类型(基本数据类型、String类型)的变量:使用Scanner
- 键盘输入代码的四个步骤:
- 导包:
import java.util.Scanner;
- 创建Scanner类型的对象:
Scanner scan = new Scanner(Sysytem.in);
- 调用Scanner类的相关方法:(
next() / nextXxx()
),来获取指定类型的变量 - 释放资源:
scan.close();
- 导包:
- 案例:小明注册某交友网站,要求录入个人相关信息。如下:请输入你的网名、年龄、体重、恋爱状态、性别等。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入你的网名:");
String name = scan.next();
System.out.print("请输入你的年龄:");
int age = scan.nextInt();
System.out.print("请输入你的体重:");
double weight = scan.nextDouble();
System.out.print("是否单身(true(是)或false(否)):");
boolean isSingle = scan.nextBoolean();
System.out.print("请输入性别:男/女:");
char gender = scan.next().charAt(0);
}
}
- Scanner类中提供了获取byte、 short、 int、 long、 float、 double、 boolean、 String类型变量的方法。
- 没有提供获取char类型变量的方法。需要使用next().charAt(0)
获取一个随机数
- 生成一个指定范围的随机数的方式:
int rendom = (int)(Math.*random*() * (b -a +1) + a);
a ~ bint rendom = (int)(Math.*random*() * (79 -60 +1) + 60);
60 ~ 70
可以使用 java.util.Random
类来获取一个随机数。
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random rand = new Random();
int randomNum = rand.nextInt(100); // 生成 0~99 之间的随机整数
System.out.println(randomNum);
}
}
在以上代码中,使用 nextInt()
方法来获取一个随机整数。如果要获取其他类型的随机数,可以使用类似的方法。
可以使用 java.util.Random
类来获取一个随机数。
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random rand = new Random();
int randomNum = rand.nextInt(30) + 1; // 生成 1~30 之间的随机整数
System.out.println(randomNum);
}
}
在以上代码中,使用 nextInt()
方法来获取一个随机整数。如果要获取其他范围的随机数,可以使用类似的方法。
可以使用 java.util.Random
类来获取一个随机数。
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random rand = new Random();
int randomNum = rand.nextInt(91) + 10; // 生成 10~100 之间的随机整数
System.out.println(randomNum);
}
}
在以上代码中,使用 nextInt()
方法来获取一个随机整数。如果要获取其他范围的随机数,可以使用类似的方法。
这一行代码使用 nextInt()
方法获取一个随机整数,范围是0~90
。然后将结果加上10
,得到的随机数的范围是10~100
。即生成一个10
到100
之间的随机整数。
分支语句(switch-case)选择结构
switch中的表达式只能是特定的数据类型。如下: byte\ short \ char \ int \ 枚举(JDK5新增) \ String(JDK7新增)。
开发中,使用switch-case时,通常case匹配的情况都有限。
default:类似于if-else中的else结构。
语法格式:
switch(表达式){
case 常量值1:
语句块1;
//break;
case 常量值2:
语句块2;
//break;
default:
语句块n;
break;
}
在 switch-case 语句中,break 语句的作用是跳出 switch 语句,结束当前的 case 块的执行。如果没有 break 语句,程序将会继续执行下一个 case 块直到遇到 break 或 switch 语句结束为止。
案例:
class Test01 {
public static void main(String[] args) {
int num = 6;
switch (num) {
case 0:
System.out.println("零");
break; //结束当前的 case 块的执行
case 1:
System.out.println("壹");
break; //结束当前的 case 块的执行
case 2:
System.out.println("贰");
break; //结束当前的 case 块的执行
default:
System.out.println("没有找到" + num);
break; //结束当前的 case 块的执行
}
}
}
switch-case 语句的执行过程:
在 switch-case
结构中,程序首先对 switch
后面的表达式进行求值,然后将其与每个 case
语句中的常量值进行比较。如果匹配成功,则程序执行该 case
语句后的语句块;如果没有匹配成功,则程序执行 default
语句块。当程序执行完语句块后,必须使用 break
语句来结束 switch
语句,否则程序将继续执行下一个 case
语句块。如果没有 default
语句块,而且没有任何一个 case
匹配成功,则程序不会执行任何语句块。
int score = 70;
switch (score / 10) {
case 10:
case 9:
case 8:
case 7:
case 6:
System.out.println("合格");
break;
default:
System.out.println("不合格");
break;
}
循环结构(for循环)
凡是循环结构,就一定会有4个要素:
- 初始化条件
- 循环条件 —>布尔类型
- 循环体
- 迭代部分
for循环格式:
for(1;2;4){
3
}
执行过程:1-2-3-4-2-3-4-……-2
案例:输出5行HelloWorld
class Test01 {
public static void main(String[] args) {
for (int i = 0;i < 5;i++){
System.out.println("HeolloWorld");
}
}
}
案例:遍历1-100以内的偶数,并获取偶数的个数,获取所有的偶数的和
class Test01 {
public static void main(String[] args) {
int count = 0 ;
int sum = 0;
for (int i = 1;i <= 100;i++){
if(i % 2 ==0){
System.out.println(i);
count++;
sum += i;
}
}
System.out.println("偶数的个数为:"+ count);
System.out.println("偶数的总和为:"+ sum);
}
}
案例:输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身。
例如:153 = 111 + 333 + 555
public class Test01 {
public static void main(String[] args) {
//遍历所有的三位数
for (int i = 100; i <= 999;i++){
//针对于每一个三位数i,获取其各个位上的数值
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai){
System.out.println(i);
}
}
}
}
案例:输入两个正整数m和n,求其最大公约数和最小公倍数。
比如: 12和20的最大公约数是4,最小公倍数是60。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入第一个正整数:");
int m = scan.nextInt();
System.out.print("请输入第二个正整数:");
int n = scan.nextInt();
int max = m > n ? m : n;
int min = m < n ? m : n;
int gcd = 1;
int lcm = 1;
for (int i = 1; i <= min; i++) {
if (max % i == 0 && min % i == 0) {
gcd = i;
}
}
lcm = max * min / gcd;
System.out.println(max + "和" + min + "的最大公约数是" + gcd);
System.out.println(max + "和" + min + "的最小公倍数是" + lcm);
}
}
循环结构(while循环)
- for循环与while循环可以相互转换!
- 初始化条件的作用域范围不同,while循环中的初始化条件在while循环结束后,依然有效。
public class Test01 {
public static void main(String[] args) {
//遍历50次HelloWorld
int i = 1;
while (i <= 50){
System.out.println("HelloWorld"+ i);
i++;
}
}
}
public class Test01 {
public static void main(String[] args) {
//遍历1-100以内的偶数,并获取偶数的个数,获取所有的偶数的和
int j = 1;
while (j <= 100){
if(j % 2 == 0){
System.out.println(j);
count++;
sum += j;
}
j++;
}
System.out.println("偶数的个数为:"+count);
System.out.println("偶数的总和为:"+sum);
}
}
案例:随机生成一个100以内的数,猜这个随机数是多少 ?
从键盘输入数,如果大了,提示大了: 如果小了,提示小了:如果对了,就不再猜了,并统计一共猜了多少次。
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
//生成一个1-100范围的随机整数
int random = (int)(Math.random() * (100 -1 +1) + 1);
//使用Scanner,从键盘获取数据
Scanner scan = new Scanner(System.in);
System.out.print("请输入1-100范围内的一个整数:");
int guess = scan.nextInt();
//声明一个变量记录猜测次数
int guessCount = 0;
//使用循环结构,进行多次循环的对比和获取数据
while (random != guess){
if (guess > random){
System.out.println("你输入的数据大了");
} else if (guess < random) {
System.out.println("你输入的数据小了");
}
System.out.println("请输入1-100范围内的一个整数:");
guess = scan.nextInt();
guessCount++;
}
//能结束,意味着random和guess相等了
System.out.println("恭喜你猜对了");
System.out.println("共猜了:"+guessCount+"次");
}
}
循环结构(do-while循环)
💡 do-while循环至少执行一次循环体 for、while、do-while循环三者之间是可以相互转换的 do-while
循环结构在开发中相较于for、while循环来讲、使用的较少
1
do{
3
4
}while(2);
1-3-4-2
案例:
public class Test02 {
public static void main(String[] args) {
//遍历100以内的偶数,并输出偶数的个数与总和
int i = 1;
int count = 0; //记录偶数的个数
int sum = 0; //记录偶数的总和
do {
if (i % 2 ==0){
System.out.println(i);
count++;
sum += i;
}
i++;
}while (i <= 100);
System.out.println("偶数的个数:"+count);
System.out.println("偶数的总和:"+sum);
}
}
“无限”循环的使用
- 格式:
while(true) 或者 for(;;)
public class Test02 {
public static void main(String[] args) {
while (true){
System.out.println("Hello");
}
}
}
public class Test02 {
public static void main(String[] args) {
for (;;){
System.out.println("Hello");
}
}
}
```r(;;)`