53_Java_流程控制
分支结构
根据条件,选择性地执行某段代码 有if…else和switch-case两种分支语句
if else if else
public class IfTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
if(score == 100){
System.out.println("a");
}else if(score > 80 && score <= 99){
System.out.println("b");
}else{
System.out.println("c");
}
}
}
switch case
*
2.说明:
① 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。
当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字
② break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构
③ switch结构中的表达式,只能是如下的6种数据类型之一:
byte 、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)
④ case 之后只能声明常量。不能声明范围。
⑤ break关键字是可选的。
⑥ default:相当于if-else结构中的else. default结构是可选的,而且位置是灵活的。
*/
public class SwithchCase {
public static void main(String[] args) {
int number = 2;
switch(number){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("other");
} }}
循环结构
根据循环条件,重复性的执行某段代码 有while、do…while、for三种循环语句
For循环结构的使用
一、循环结构的4个要素
1 初始化条件 ; 2 循环条件 --->是boolean类型
3 循环体; 4 迭代条件
二、for循环的结构
for(1;2;4){
3
}
执行过程:1 - 2 - 3 - 4 - 2 - 3 - 4 - ... - 2
public class ForTest {
public static void main(String[] args) {
int sum = 0;//记录所有偶数的和
int count = 0;//记录偶数的个数
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
}
System.out.println("总和为:" + sum);
System.out.println("个数为:" + count);
}
}
While 循环的使用
一、循环结构的4个要素
1 初始化条件 2 循环条件 --->是boolean类型
3 循环体 4 迭代条件
二、while循环的结构
1
while(2){
3;
4;
}
执行过程:1 - 2 - 3 - 4 - 2 - 3 - 4 - ... - 2
public class WhileTest {
public static void main(String[] args) {
int i =1;
while (i <=100){
if(i % 2 == 0){
System.out.println(i);
}
i++;
}
System.out.println(i); //101
}
}
do-while循环的使用
一、循环结构的4个要素
1 初始化条件 2 循环条件 --->是boolean类型
3 循环体 4 迭代条件
二、do-while循环结构:
1
do{
3;
4;}while(2);
执行过程:1 - 3 - 4 - 2 - 3 - 4- ... - 2
说明:
1.do-while循环至少会执行一次循环体!
2.开发中,使用for和while更多一些;较少使用do-while
public class DoWhileTest {
public static void main(String[] args) {
int num = 1;
int sum = 0;//记录总和
int count = 0;//记录个数
do{
if(num % 2 == 0){
// System.out.println(num);
sum += num;
count++;
}
num++;
}while(num <= 100);
System.out.println(sum); //50
System.out.println(count); //2550
}
}
嵌套循环的使用
1.嵌套循环:将一个循环结构A声明在另一个循环结构B的循环体中,就构成了嵌套循环
2. 外层循环:循环结构B; 内层循环:循环结构A
3. 说明
1 内层循环结构遍历一遍,只相当于外层循环循环体执行了一次
2 假设外层循环需要执行m次,内层循环需要执行n次。此时内层循环的循环体一共执行了m * n次
4. 技巧: 外层循环控制行数,内层循环控制列数
/*
100000以内的所有质数的输出。实现方式二
质数:素数,只能被1和它本身整除的自然数。-->从2开始,到这个数-1结束为止,都不能被这个数本身整除。
对PrimeNumberTest.java文件中质数输出问题的优化
*/
class PrimeNumberTest2 {
public static void main(String[] args) {
int count = 0;//记录质数的个数
//获取当前时间距离1970-01-01 00:00:00 的毫秒数
long start = System.currentTimeMillis();
label:for(int i = 2;i <= 100000;i++){//遍历100000以内的自然数
for(int j = 2;j <= Math.sqrt(i);j++){//j:被i去除
if(i % j == 0){ //i被j除尽
continue label;
}
}
//能执行到此步骤的,都是质数
count++;
}
//获取当前时间距离1970-01-01 00:00:00 的毫秒数
long end = System.currentTimeMillis();
System.out.println("质数的个数为:" + count);
System.out.println("所花费的时间为:" + (end - start));//17110 - 优化一:break:1546 - 优化二:13
}
}
- 点赞
- 收藏
- 关注作者
评论(0)