Java自动化测试(循环/条件控制语句 预习3)

Java自动化测试(循环/条件控制语句 预习3)

for循环

格式

1
2
3
for(初始化值1;循环终止条件2;循环体执行后的语句4){
循环体3;
}
1
2
3
for (int i = 0; i <= 10; i++) {
System.out.println(i);
}

流程

初始化值1->循环终止条件2「true」->循环体3->循环体执行后的语句4 ->循环终止条件2「true」->循环体3->循环体执行后的语句4->…循环终止条件2「false」-> 循环结束

增强for循环

for(内部元素类型 变量名;集合){}

普通方式实现

1
2
3
4
int[] arr = {100, 24, 90, 78};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

增强方式实现

  • 优点:代码少
  • 缺点:不能直接获取索引
1
2
3
for (int i : arr) {
System.out.println(i);
}

求和

1
2
3
4
5
6
7
8
9
10
11
package com.zhongxin.loop;

public class ForDemo2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 10; i++) {
sum += i;
}
System.out.println(sum);
}
}

while循环

1
2
3
4
5
6
7
8
9
10
11
package com.zhongxin.loop;

public class WhileDemo {
public static void main(String[] args) {
int i = 1; // 初始化语句1
while (i <= 10) { // 循环执行条件2
System.out.println(i); // 循环体3
i++; // 循环体执行完毕之后的语句4
}
}
}

流程:1234->2(true)->34->2(true)->......->2(false)->循环结束

Do…While

至少会执行一次循环体,其他和while一样

1
2
3
4
5
6
7
8
9
10
11
package com.zhongxin.loop;

public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

break

跳出整个循环

1
2
3
4
5
6
7
8
for (int i = 1; i <= 10; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
// 1
// 2

continue

结束本次循环,继续下次循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.zhongxin.loop;

public class BreakContinueDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
}
}
// 1
// 2
// 4
// 5
// 6
// 7
// 8
// 9
// 10

条件控制语句

  • switch
  • if … else
  • if else if … else
  • if … if … if

if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.zhongxin.ifdemo;

public class IFDemo {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("恭喜你成年了");
}
if (age >= 18) {
System.out.println("可以访问本网站");
}else{
System.out.println("FBI WARNING!");
}

int score = 60;
if (score == 100) {
System.out.println("满分");
}else if (score>=90){
System.out.println("优秀");
}else if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}

switch

优点

效率比if快

结束的条件

  • switch
  • 右大括号

变量类型

  • <= int
  • String
  • 枚举
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.zhongxin.ifdemo;

public class SwitchDemo {
public static void main(String[] args) {
int i = 1;
switch (i) {
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("未知");
break;
}
}
}

不使用break 形成穿透

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.zhongxin.ifdemo;

public class SwitchDemo2 {
public static void main(String[] args) {
int mouth = 12;
switch (mouth) {
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;
case 12:
case 1:
case 2:
System.out.println("冬");
break;
default:
System.out.println("未知");
break;
}
}
}

练习

打印

1
2
3
4
5
*
**
***
****
*****
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.zhongxin.ifdemo;

public class Homework {
public static void main(String[] args) {
String start = "*";
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
System.out.print(start);
}
System.out.println("");
}
}
}

打印

1
2
3
4
5
    *  
* *
* * *
* * * *
* * * * *
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.zhongxin.ifdemo;

public class Homework1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int x = 1; x <= i * 2 - 1; x += 2) {
System.out.print("* ");
}
System.out.println();
}
}
}
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!