> 文档中心 > 《JAVASE系列》循环与分支与小游戏实现

《JAVASE系列》循环与分支与小游戏实现


提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 《JAVASE系列》循环与分支与小游戏实现
    • 前言:
    • 一.分支结构
    • 二.循环结构
      • 1. while循环
      • 2. break
      • 3. continue
      • 4. for循环
      • 5. do while 循环(了解)
    • 三.猜数字游戏
    • 总结:

《JAVASE系列》循环与分支与小游戏实现

《JAVASE系列》循环与分支(0基础必看)

前言:

本章主要讲解 JAVA的循环与分支的基础知识,并在最后为大家实现一个小游戏。

程序与人生一样,顺序中夹杂着循环,处处充满了选择。

参考书籍:《java核心卷1》

你若盛开,清风自来。

一.分支结构

1. if 语句

语法格式:

if(布尔表达式){    语句1}else if(布尔表达式){    语句2}else{    语句3}

表达式1成立,执行语句1,否则表达式2成立,执行语句2,否则执行语句3

代码示例:

public class test {    public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); if (score >= 90) {     System.out.println("优秀"); } else if (score >= 80 && score < 90) {     System.out.println("良好"); } else if (score >= 70 && score < 80) {     System.out.println("中等"); } else if (score >= 60 && score < 70) {     System.out.println("及格"); } else if (score >= 0 && score < 60) {     System.out.println("不及格"); } else {     System.out.println("错误数据"); }    }}

运算结果:

在这里插入图片描述

垂悬else问题

在java语法中,else总是与最近的if相匹配。所以我们要尽量避免出现以下代码

public class test {    public static void main(String[] args) { int x = 10; int y = 10; if (x == 10)     if (y == 10)  System.out.println("aaa");  else      System.out.println("bbb");    }}

代码理解:虽然else与第一个 if 在形式上看是相匹配的,但是本质上是与第二个if相匹配的。

为了避免这种操作,在使用 if 语句时,都建议带上花括号{ },避免代码出现歧义,也能让我们写出高质量的代码。

2.switch语句

语法规则:

switch(表达式){ case 常量值1:{ 语句1; break; } case 常量值2:{ 语句2; break; } ... default:{   }

代码理解:

public class test {    public static void main(String[] args) { int a = 0; char b = 0; float c = 0; double d = 0; long e = 0; byte f = 0; short g = 0; String j = "abd"; boolean h = true; switch(a) { } switch(b) { } switch(c) { } switch(d) { } switch(e) { } switch(f) { } switch(g) { } switch(j) { } switch(h) { }    }}

我们可以在编译器中就可以看到其报错警告了:

在这里插入图片描述

即switch()中的表达式不可以是以下四种类型:

float,double,boolean,long

switch的代码示例:

public class test {    public static void main(String[] args) { int day = 1; switch(day) {     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("输入错误");  break;   }    }}

运行结果:
在这里插入图片描述

【注意事项】

  • 多个case后的常量值不可以重复
  • 强调:switch的表达式不可以包含:float,double,boolean,long
  • switch 虽然支持嵌套, 但是很丑,一般不推荐。
  • break切记别遗漏,否则会失去多分支的功能。

二.循环结构

1. while循环

语法简介:

while(循环条件){ 循环语句;}

循环条件为 true, 则执行循环语句; 否则结束循环.

代码示例:

输出1 - 10

public class test {    public static void main(String[] args) { int n = 0; while(n<10) {     System.out.println(n);     n++; }    }}

运行结果:

在这里插入图片描述

代码示例:

计算1!+ 2! + 3!+ 4!+ 5!

循环嵌套

public class test {    public static void main(String[] args) { int num = 1; int sum = 0; // 外层循环负责求阶乘的和 while (num <= 5) {   int factorResult = 1;   int tmp = 1; // 里层循环负责完成求阶乘的细节.  while (tmp <= num) {      factorResult *= tmp;      tmp++;  }   sum += factorResult;   num++; } System.out.println("sum = " + sum);    }   }

运行结果:

在这里插入图片描述

注意事项:

  1. 和 if 类似, while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
  2. 和 if 类似, while 后面的 { 建议和 while 写在同一行.
  3. 和 if 类似, while 后面不要多写 分号, 否则可能导致循环不能正确执行.

2. break

break 的功能是让循环提前结束.

代码示例:

找到 100 - 200 中第一个 3 的倍数

public class test {    public static void main(String[] args) { int num = 100; while (num <= 200) {   if (num % 3 == 0) {  System.out.println("找到了 3 的倍数, 为:" + num); break;    }    num++; }    }}

运行结果:

在这里插入图片描述

执行到 break 就会让循环结束.

3. continue

continue 的功能是跳过这次循环, 立即进入下次循环.

代码示例: 找到 100 - 200 中所有 3 的倍数

int num = 100;while (num <= 200) {    if (num % 3 != 0) { num++;  // 这里的 ++ 不要忘记! 否则会死循环.  continue;   }    System.out.println("找到了 3 的倍数, 为:" + num);    num++;}

运行结果:

在这里插入图片描述

执行到 continue 语句的时候, 就会立刻进入下次循环(判定循环条件), 从而不会执行到下方的打印语句.

4. for循环

语法简介:

for(表达式①;布尔表达式②;表达式③){ 表达式④}

执行过程:

①②③④—>②③④—>②③④—>②③④—>②③④—>②③④—>…—>②为false,循环结束。

代码示例:

计算 5 的阶乘

public class test {    public static void main(String[] args) {      int result = 1;      for (int i = 1; i <= 5; i++) {   result *= i;      }      System.out.println("result = " + result);    }}

可以自己按for循环的规则推导一下
计算结果:
在这里插入图片描述

注意事项: (和while循环类似)

  1. 和 if 类似, for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }( 代码规范 )
  2. 和 if 类似, for 后面的 { 建议和 while 写在同一行.
  3. 和 if 类似, for 后面不要多写 分号, 否则可能导致循环不能正确执行.
  4. 和while循环一样,结束单趟循环用continue,结束整个循环用break.

5. do while 循环(了解)

先执行循环语句, 再判定循环条件,循环条件成立则继续执行,否则循环结束。

   int num = 1;   do {System.out.println(num);num++;    } while (num <= 10);

注意事项:

  1. do while 循环最后的分号不要忘记
  2. 一般 do while 很少用到, 更推荐使用 for 和 while.

三.猜数字游戏

public class test {    public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random();//随机数入口 int randNum = random.nextInt(50) + 51; //[50,100)  -> [1,101)  生成1-100的随机数 while (true) {     System.out.println("请输入你要猜的数字:");     int num = scanner.nextInt();     if (num < randNum) {  System.out.println("猜小了!");     } else if (num == randNum) {  System.out.println("猜对了!");  break;     } else {  System.out.println("猜大了!");     } }    }}

运行结果:

在这里插入图片描述

在这里插入图片描述

总结:

本章主要通过代码示例以及一个小游戏来学习循环与分支的语法。如果想要更多的巩固java语法还是要多去做编程题。

感谢看完!

与君共勉!