4. 流程控制语句
网络安全JavaSE第三天
4. 流程控制语句
4.1 if条件语句
如果条件表达式 true,就执行 if 的语句代码块。它有以下几种格式:
// 第一种: if (条件表达式) { 执行的语句; } // 第二种: if (条件表达式) { 表达式的值为真时执行的语句; } else { 表达式的值为假时执行的语句; } // 第三种: if (条件表达式1) { 表达式1的值为真时执行的语句; } else if (条件表达式2) { 表达式2的值为真时执行的语句; } else { 其他条件时要执行的语句; }
使用示例,只有一个语句:
public class IfStatement01 { public static void main(String[] args) { // 接收用户输入 Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); // 接收一个整数 int input = sc.nextInt(); if (input == 18) { System.out.println("你猜对了。"); } } }
使用示例,有两个语句:
import java.util.Scanner; /** * if...else...语句 */ public class IfStatement02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你猜的年龄:"); int age = sc.nextInt(); if (age == 18) { System.out.println("猜对了。"); } else { System.out.println("再给你一个机会"); } } }
使用示例,多条件表达:
import java.util.Scanner; /** * 多条件表达式 */ public class IfStatement03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入今天是周几:"); int week = sc.nextInt(); if (week == 1) { System.out.println("今天是星期一"); } else if (week == 2) { System.out.println("今天是星期二"); } else if (week == 3) { System.out.println("今天是星期三"); } else if (week == 4) { System.out.println("今天是星期四"); } else if (week == 5) { System.out.println("今天是星期五"); } else if (week == 6) { System.out.println("今天是星期六"); } else if (week == 7) { System.out.println("今天是星期七"); } else { System.out.println("你一定不是来自地球!"); } } }
4.2 switch分支语句
switch语句的语法如下:
switch(表达式) { case 值1: 语句1; break; case 值2: 语句2; break; ..... default: 默认语句块; break; }
使用示例:
import java.util.Scanner; /** * Switch语句 */ public class SwitchStatement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("今天是星期几:"); int week = sc.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("地球很危险,你还是回火星吧"); break; } } }
注意:
case 语句块中,break 不能省略,否则会继续向下执行,直接遇到 break 或者 switch 代码块结束。
default 语句块是可以在 swtich 语句块中任何位置,不一定就是在最后。但如果不是在最后,那么代码块中的 break 语句不能省略
如果default语句块是在最后,那么 break 语句可以省略
if 语句块和 swtich 语句块的区别:
-
switch语句比if语句的执行效率高
-
if语句的适用场景比switch要多
4.3 for循环
当我们需要重复去做某些事(功能)时,就可以考虑使用循环来实现。在 Java中循环有以下几种:
-
for 循环
-
while 循环
-
do..while循环
对于循环来说它有以下几个部分组成:
-
循环变量初始,在整个循环中只会执行一次
-
循环条件
-
循环体,它就是我们要反复执行代码
-
循环变量的步长,它是用来改变循环变更,从而可以达到退出循环的目的
for循环的简单使用:
1)使用格式:
for (循环变量类型 循环变量 = 初值; 循环条件; 循环变量步长) { 循环体; }
2)使用示例:使用 for 循环实现 1 ~ 100 的和
public class ForDemo02 { public static void main(String[] args) { int sum = 0; // 存放最终的结果 for (int i = 1; i <= 100; i++) { //sum = sum + i; sum += i; // +=、-=、/=、*=、%= } System.out.println("sum = " + sum); } }
3)示例3:计算 1 ~ 100 的偶数和
public class ForDemo03 { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { if (i % 2 == 0) { sum += i; } } System.out.println("sum = " + sum); System.out.println("------------------"); sum = 0; for (int i = 2; i <= 100; i = i + 2) { sum += i; } System.out.println("sum = " + sum); } }
对于 for 循环来说,还可以做如下的变化:
public class ForDemo04 { public static void main(String[] args) { int sum = 0; int i = 2; for (; i <= 100; ) { i = i + 2; sum += i; } System.out.println("sum = " + sum); } }
for 循环的死循环写法:
public class ForDemo05 { public static void main(String[] args) { for (;;) { System.out.println("hello"); } } }
for循环是可以嵌套的,它的语法格式为:
for (循环变量类型 循环变量 = 初值; 循环条件; 循环变量步长) { for (循环变量类型 循环变量 = 初值; 循环条件; 循环变量步长) { 循环体; } }
案例1:打印四边形
public class ForDemo06 { public static void main(String[] args) { // 打印5行10列的长方形 for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { System.out.print(" * "); } System.out.println(); } } }
案例2:打印如下三角形
# ## ### #### ##### ######
public class ForDemo07 { public static void main(String[] args) { for (int i = 0; i <= 6; i++) { for (int j = 0; j <= i; j++) { System.out.print("#"); } System.out.println(); } } }
###### ##### #### ### ## #
public class ForDemo08 { public static void main(String[] args) { for (int i = 1; i <= 6; i++) { for (int j = 6; j >= i; j--) { System.out.print("#"); } System.out.println(); } } }
4.4 while循环
while循环的结构如下:
循环变量初值; while(循环条件) { 要执行的逻辑; 循环变量步长; }
使用案例:计算 1 ~ 100 的和
public class WhileDemo01 { public static void main(String[] args) { // 需要:计算 1 ~ 100 的和 int sum = 0; // 定义循环变量初值 int i = 1; // 循环 while (i <= 100) { sum += i; // sum = sum + i; i++; } System.out.println("sum = " + sum); } }
这个代码还可以简化一下下:
package com.openlab; public class WhileDemo01 { public static void main(String[] args) { // 需要:计算 1 ~ 100 的和 int sum = 0; // 定义循环变量初值 int i = 1; // 循环 while (i <= 100) { sum += i++; // sum = sum + i; //i++; } System.out.println("sum = " + sum); } }
示例二:使用while打印九九乘法表
1 * 1 = 1 1 * 2 = 2 2 * 2 = 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
public class NineTableForWhile { public static void main(String[] args) { // 定义外层循环的初值 int i = 1; while (i <= 9) { // 定义内层循环的初值 int j = 1; while (j <= i) { System.out.print(i + " * " + j + " = " + (i * j) + "\t"); j++; } i++; System.out.println(); } } }
4.5 do...while循环
此循环的结构如下:
循环变量初值; do { 循环处理逻辑; 循环变量步长; } while (循环条件);
使用示例:计算 1~100的和
public class DoWhileDemo01 { public static void main(String[] args) { // 需求:计算1~100的和 int sum = 0; int i = 1; do { sum += i; i++; } while (i <= 100); System.out.println("sum = " + sum); } }
这三个循环的适用场景:
-
大多数情况下这三个循环都可以通用。
-
一般情况下在循环次数确定的时候通常会优先使用 for 循环,而循环次数不确定时通常会优先选择 while 循环
-
如果在任何情况下都希望至少执行一次,则选择 do...while 循环。
对于for循环和while循环来说,它们是先判断再执行,而 dowhile循环是先执行再判断。
4.6 break、continue关键字
4.6.1 break
这个关键是中用于结束循环或都退出switch,它只能用于 swtich 和 循环中。
使用示例:循环 1 ~ 10,但是当循环到 6 时就退出。
public class BreakDemo01 { public static void main(String[] args) { // 需求:循环 1 ~ 10,但是当循环到 6 时就退出。 for (int i = 1; i <= 10; i++) { //if (i < 6) { // System.out.println(i); //} System.out.println("----"); if (i >= 6) break; else System.out.println(i); } } }
除了可以退出 break 关键字所在的循环外,还可以使用它来退出指定的循环。
package com.openlab; public class BreakDemo02 { // 需求:当 j 的值为 3 时,退出外层循环 public static void main(String[] args) { out:for (int i = 1; i <= 5; i++) { System.out.println(i); for (int j = 1; j <= 5; j++) { if (j == 3) break out; System.out.println("\t" + j); } } } }
4.6.2 continue
这个关键字只能使用在循环中,来结束本次循环进入到下一次循环。
使用示例:
package com.openlab; public class ContinueDemo01 { // 需求:当循环到 5 时就跳过 public static void main(String[] args) { for (int i = 1; i < 10; i++) { if (i == 5) continue; System.out.println(i); } } }
这个关键也是可以带标签跳过的。
public class ContinueDemo02 { // 需求:当 j = 3 时结束外层循环一次,并进入到下一次。 public static void main(String[] args) { out:for (int i = 1; i <= 5; i++) { System.out.println(i); for (int j = 1; j <= 5; j++) { if (j == 3) continue out; System.out.println("\t" + j); } } } }
上次的作业
第一题:
第二题:
第三题:
package com.openlab; import java.util.Random; import java.util.Scanner; public class Exercise03 { public static void main(String[] args) { // 1. 创建一个能够产生随机数的对象 Random random = new Random(); // 2. 调用这个对象的相关方法来生成一个随机数 // 它会产生一个 [0 ~ 99) 的数 int num = random.nextInt(89) + 11; System.out.println(num); // 3. 获取个位数和十位数 int ten_digit_number = num / 10; System.out.println("ten_digit_number = " + ten_digit_number); int single_digit = num % 10; System.out.println("single_digit = " + single_digit); // 4. 接收用户的输入 // 4.1 接收用户输入 Scanner sc = new Scanner(System.in); System.out.println("请输入你要购买的数字:"); int input = sc.nextInt(); // 4.2 判断用户输入的数字是否在安全范围之内 if (input >= 100 || input < 10) { System.out.println("输入的数字不能超过100,也不能小于 10"); // 退出 System.exit(1); } // 4.3 获取用户输入数字的个位和十位 int user_ten_digit_number = input / 10; System.out.println("user_ten_digit_number = " + user_ten_digit_number); int user_single_digit = input % 10; System.out.println("user_single_digit = " + user_single_digit); // 5. 判断输入的值与产生的随机数进行匹配 if (num == input) { System.out.println("恭喜你中大奖了,得了10000美元"); } else if (ten_digit_number == user_single_digit && single_digit == user_ten_digit_number) { System.out.println("恭喜你得了二等奖,奖金为3000美元"); } else { if (ten_digit_number == user_single_digit || ten_digit_number == user_ten_digit_number || user_single_digit == single_digit ) { System.out.println("还不错,得了三等奖,奖金为 1000美元"); } else { System.out.println("差一点就中奖, 继续?"); } } } }
第四题 :
5. 常用类
5.1 Math
Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
自带常量
-
static double E :比任何其他值都更接近 e(即自然对数的底数)的
double
值。 -
static double PI : 比任何其他值都更接近 pi(即圆的周长与直径之比)的
double
值。
取整方法
-
static double ceil(double a) :返回最小的(最接近负无穷大)
double
值,该值大于等于参数,并等于某个整数。 -
static double floor(double a) :返回最大的(最接近正无穷大)
double
值,该值小于等于参数,并等于某个整数。 -
static double rint(double a) :返回最接近参数并等于某一整数的
double
值。 -
static long round(double a) :返回最接近参数的
long
。
三角函数方法
-
static double sin(double a) :返回角的三角正弦。
-
static double cos(double a) :返回角的三角余弦。
-
static double tan(double a) :返回角的三角正切。
-
static double toDegrees(double angrad) :将用弧度表示的角转换为近似相等的用角度表示的角。
-
static double toRadians(double angdeg) :将用角度表示的角转换为近似相等的用弧度表示的角。
-
static double asin(double a) :返回一个值的反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
-
static double acos(double a) :返回一个值的反余弦;返回的角度范围在 0.0 到 pi 之间。
-
static double atan(double a) :返回一个值的反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
指数函数方法
-
static double exp(double a) :返回欧拉数 e 的
double
次幂的值。 -
static double log(double a) :返回
double
值的自然对数(底数是 e)。 -
static double log10(double a) :返回
double
值的底数为 10 的对数。 -
static double pow(double a, double b) :返回第一个参数的第二个参数次幂的值。
-
static double sqrt(double a) :返回正确舍入的 double 值的正平方根。
-
static double cbrt(double a) :返回
double
值的立方根。
其他方法
-
static double abs(double a) :返回
double
值的绝对值。 -
static double hypot(double x, double y) :返回 sqrt(x2 +y2),没有中间溢出或下溢。
-
static double max(double a, double b) :返回两个
double
值中较大的一个。 -
static double min(double a, double b) :返回两个
double
值中较小的一个。 -
static double random() :返回带正号的
double
值,该值大于等于0.0
且小于1.0
。
使用示例1:获取指定数据的最大值和最小值
public class MathDemo01 { public static void main(String[] args) { int a = 5; int b = 10; int max = Math.max(a, b); System.out.println("max = " + max); int min = Math.min(a, b); System.out.println("min = " + min); } }
使用示例2:产生随机数
public class MathDemo02 { public static void main(String[] args) { double random = Math.random(); System.out.println(random); } }
得到的是 0 ~1 之间的小数,不会超过 1。
使用示例3:对小数相关操作。
package com.openlab; /** * static double ceil(double a) :返回最小的(最接近负无穷大)`double` 值,该值大于等于参数,并等于某个整数。 * static double floor(double a) :返回最大的(最接近正无穷大)`double` 值,该值小于等于参数,并等于某个整数。 * static long round(double a) :返回最接近参数的 `long`。 */ public class MathDemo03 { public static void main(String[] args) { double d1 = 3.189; double d2 = 3.897; double d3 = -3.189; double d4 = -3.897; // ceil 方法是得到比给定值大的最小的整数 double c1 = Math.ceil(d1); // 4.0 double c2 = Math.ceil(d2); // 4.0 double c3 = Math.ceil(d3); // -3 double c4 = Math.ceil(d4); // -3 System.out.println(c1 + "\t" + c2 + "\t" + c3 + "\t" + c4); // floor 得到给值小的最大的整数 double f1 = Math.floor(d1); // 3 double f2 = Math.floor(d2); // 3 double f3 = Math.floor(d3); // -4 double f4 = Math.floor(d4); // -4 System.out.println(f1 + "\t" + f2 + "\t" + f3 + "\t" + f4); long r1 = Math.round(d1); // 3 long r2 = Math.round(d2); // 4 long r3 = Math.round(d3); // -3 long r4 = Math.round(d4); // -4 System.out.println(r1 + "\t" + r2 + "\t" + r3 + "\t" + r4); } }