(四)流程控制

流程控制

流程控制中包含这几种结构:顺序结构、分支结构和循环结构。

1、顺序结构

顺序结构比较简单,是指代码从上到下从左到右依次运行的。

2、分支结构

分支结构分为判断结构和选择结构。

1.判断结构

if

格式:if( 逻辑值 ){ code;}
这里写图片描述
如果逻辑值为true,执行对应的代码块,反之则不执行。
注意:if后面的{}。只有一句代码的时候可以省略。

import java.util.Scanner;//导包语句,位置要在class之外。
public class IfDemo {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        if(i > 10){//if语句
        System.out.println("hahah~~~");
        }
        /*
        //上面的if语句里面只有一句代码,就可以将if写为下面这种形式。
        if(i > 10)
        System.out.println("hahah~~~")
        */
    }
}
练习

判断三个数中的最大数。

if–else

格式:if(逻辑值){Code1;} else {Code2;}
如果逻辑值为true,执行Code1;反之执行Code2。
这里写图片描述

//判断奇偶
import java.util.Scanner;
public class IfElseDemo {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        if(i % 2 == 1){
            System.out.println(i + "is an odd number .");
        } else {
            System.out.println(i + "is an even number .");
        }
    }
}
练习

输入三个数,获取最大值

import java.util.Scanner;
public class Test1{
    public static void main(String[] args){
    Scanner s=new Scanner(System.in);
    int num1=s.nextInt();
    int num2=s.nextInt();
    int num3=s.nextInt();
    int max;
    if(num1>num2){
        if(num1>num3){
            max=num1;
        }else{
            max=num3;
        }
    }else{
        if(num2>num3){
            max=num2;
        }else{
            max=num3;
        }
    }
    System.out.println(max+" is max.");
    }
}

上面的代码是我自己练习的时候写的,比较冗余,之后就没做过更改。

If–else if–else

格式:
if(逻辑值1){
Code1;
} else if(逻辑值2){
Code2;
}…else {
Code;
}
如果逻辑值1为true,执行Code1;如果逻辑值1为false,则判断逻辑值2是否为true,如果逻辑值2是true,则执行Code2。

import java.util.Scanner;
public class IfElseIfDemo {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        double score = s.nextDouble();
        if(score > 100||scpre < 0){
            System.out.println(erres);
        }else if(score >= 90){
            System.out.println("A");
        } else if(score >= 80 ){
            System.out.println("B");
        } else if(score >= 60){
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}
练习

输入一个月份,输出月份对应的季节 3-5-Spring 6-8-Summer 9-11-Autumn 12-2-Winter

import java.util.Scanner;
public class Test2{
    public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        int month=s.nextInt();

        String season = "";
        if(month>=13||month<=0){
            season ="Wrong month !";
        }else if(month>=3&&month<=5){
            season ="Spring";
        }else if(month>=6&&month<=8){
            season = "Summer";
        }else if(month>=9&&month<=11){
            season ="Autumn";
        }else{
            season ="Winter";
        }
        System.out.println(season);
    }
}

2.选择结构

switch

格式:
switch(表达式){
case 选项1: code1; break;
case 选项2: code2; break;
……
default: code;break;
}
注意:
(1)表达式的值只能是byte/short/char/int四种类型,JDK1.7开始,支持对String进行选择,从JDK1.5开始允许表达式的结果是枚举常量。
(2)break对多行case代码有影响。
如果case之后没有break,那么从匹配的case开始,依次往下执行,直到遇到break或者是switch语句结束。
如果每个case之后都有break的话,case之间的顺序不影响最后的结果;即使有一个case没有break,这个时候case之间的顺序会影响最后的结果。

明坑题
int i = s.nextInt();
// int j ;
switch(i){
    case 1 : int j = 0;
    case 2 : j += 1; // 编译报错  可能尚未声明变量j
    case 3: j += 3;
    default : System.out.println(j);
}
练习

计算器
一个符号和两个数字 - 5 9-> 5 - 9 = -4

import java.util.Scanner;
public class SwitchCaseExer {
    public static void main(String[] args){ 
        Scanner s = new Scanner(System.in);     
        String symbol = s.nextLine();
        int i = s.nextInt();
        int j = s.nextInt();        
        switch(symbol){
            case "+" : 
                System.out.println(i + j);
                break;
            case "-" :
                System.out.println(i - j);
                break;
            case "*" :
                System.out.println(i * j);
                break;
            case "/" :
                System.out.println(i / j);
                break;
            default:
                System.out.println("No this operation !");
        }
    }
}

3、循环结构

循环三个要素:循环变量,循环条件,变量改变。

注意:尽量不要使用浮点型来作为循环条件,因为小数在计算机中不能够精确存储。

1.while

格式:
while(判断循环变量){
code;
}
这里写图片描述

public class WhileDemo {
    public static void main(String[] args){
        int i = 1;
        while(i <= 5){
            System.out.println("Hello");
            i++;
        }
    }
}
练习

1.从控制台输入一个数字,求1-这个数字的和。

import java.util.Scanner;
public class WhileExer1 {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        /*
        int i = 1;
        int sum = 0;
        while(i <= n){
        sum = sum + i;
        i++;
    }
    */
        int sum = 0;
        while(n > 0){
            sum += n;
            n--;
        }
        System.out.println(sum);
    }
}

2.输出500以内8的倍数。

int i = 0;
while(i <= 500){
    System.out.println(i);
    i += 8;
}
/*
while(i <= 500){
    if(i % 8 == 0){
    System.out.println(i);
    }
    i++;
}
*/

3.输入一个数字,输出这个数字是几位数—3463-4。

Scanner s = new Scanner(System.in);
int number = s.nextInt();
int i = 0;
while(number != 0){
    number /= 10;
    i++;
}
System.out.println(i);

4.输入一个数字,输出各位数字之和— 3463 - 16。

Scanner s = new Scanner(System.in);
int number = s.nextInt();
int sum = 0;
while(number != 0){
    sum += (number % 10);
    number /= 10;
}
System.out.println(sum);

5.输入一个数字,输出这个数的二进制。

Scanner s=new Scanner(System.in);
int num =s.nextInt();
String str="";
while(num!=0){
    str= num % 2 + s;
    num/=2;
}
System.out.println(str);

6.求PI的计算次数

public static void main(String[] args) {
    int i=0;
    int base =1;
    double PI = 0;
    double sign =1.0;
    while(PI >3.1415927 || PI < 3.1415926){

        PI = PI + 4 * sign/ base;
        i++;
        sign *= -1;
        base += 2;
    }
    System.out.println(i);
}

2.do–whlie

格式:
do{
Code;
}while(逻辑值);
这里写图片描述

int i = 20;
/*
while(i <= 10){
    System.out.println(i);
    i++;
}
*/
do{
    System.out.println(i);
    i++;
}while(i <= 10);

注意:do–whlie循环中代码块至少执行一次。

3.for

格式:
for(定义循环变量;循环条件的判断;循环变量的改变){
Code;
}
这里写图片描述
for循环执行的顺序为:
1.定义循环变量
2.循环条件判断
3.执行内部code
4.执行循环变量的改变
5.执行上述2,3,4,2,3,4……直到循环条件判断为false然后跳出循环。

当循环变量改变相对比较规律的时候,建议使用for循环。当不知道循环次数的时候,建议使用while。
while循环,循环变量定义在循环外,循环结束之后循环变量依然可以使用;for循环的循环变量是定义在for循环里边的,循环结束之后,循环变量不能继续使用。当然for循环的变量也可以定义在for循环之外。

for(int i = 1; i <= 5; i++){
    System.out.println("Hello");
}

int sum = 0;
for(int i = 1; i <= 100; i++){
    sum += i;
}
练习

求一个数二进制的1的个数。
一个int 类型的整数由32 个二进制位组成,每个二进制位的值要么为0 要么为1。要求读入一个int 类型的整数n,计算它的32 个二进制位中总共有多少位为1?

public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int number =s.nextInt();
    s.close();

    int j=0;
    for (int i = 0; i < 32; i++) {
        if((number &1)==1){
        j++;
    }
    number >>=1;
    }
    System.out.println(j);
}

4.循环的嵌套

循环结构中包含了其他的循环结构,形成了循环的嵌套。
一般我们打印图形,有如下的方式:

public class ForDemo2 {
public static void main(String[] args){
/*
*****
*****
*****
*****
*/

    for(int j = 1; j <= 4; j++){
        for(int i = 1; i <= 5; i++){
            System.out.print("*");
        }
        System.out.println();
    }

/*
*
**
***
****
*****
row: 1 -> 5
*: 1 -> i
*/

    for(int i = 1; i <= 5; i++){
        for(int j = 1; j <= i; j++){
            System.out.print("*");
        }
        System.out.println();
    }


/*
*****
****
***
**
*
*/

    for(int i = 5; i > 0; i--){

        for(int j = i ; j > 0; j--){
            System.out.print("*");
        }
        System.out.println();
    }

/*
----*
---**
--***
-****
*****
row:1-5
" ": 1 -> 5 - i
*: 1 -> i
*/
    for(int i = 1; i <= 5; i++){
        for(int j = 1; j <= 5 - i; j++){
            System.out.print(" ");
    }
    for(int j = 1; j <= i; j++){
        System.out.print("*");
    }
    System.out.println();
    }
/*
*****
****
***
**
*
row:5 -> 1
" ": 5 - i -> 1
* : i -> 1
*/

    for(int i = 5; i > 0; i--){
        for(int j = 5 - i; j > 0; j--){
            System.out.print(" ");
        }
        for(int j = i; j > 0; j--){
            System.out.print("*");
        }
        System.out.println();
    }

}
练习

九九乘法表
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9

for(int i = 1; i <= 9; i++){
    for(int j = 1; j <= i; j++){
        System.out.print(j + "*" + i + "=" + i * j + "\t");
    }
    System.out.println();
}

百钱买百鸡
3文钱可以买1只公鸡,2 文钱可以买一只母鸡,1 文钱可以买3 只小鸡。用100 文
钱买100只鸡,那么各有公鸡、母鸡、小鸡多少只?
水仙花数
所谓水仙花数,是指一个三位数abc,如果满足a3+b3+c3=abc,则abc 是水仙花数。(善用%)

5.break和continue

break

作用于选择结构和循环结构,表示终止当前的一层语句。

continue

只能作用于循环结构,表示跳出当前的一次循环继续下次循环。
这两个关键字必须结合相应的语句使用个,单独存在没有意义,因此单独存在的时候编译报错。
Java中,不允许废话存在,存在废话就会编译报错。

循环标号

Java中支持对循环进行标号(要求看懂即可)。
当需要一次性跳出多层循环的时候,可以使用标号形式,但是不推荐使用,因为降低了程序的阅读性。

A:for(int i = 0 ; i < 3; i++){  //A为标号
    B:for(int j = 0; j < 4; j++){
        for(int k = 1; k < 6; k++){
            if(k == 3){
                break A;
            }
            If(k==1)
                continue B;
            System.out.println(i + "," + j + "," + k);
        }
    }
}
练习

1.判断质数
一个数除了1和本身不能整除其他的数。(使用Math.sqrt()注意2和3的判断。)
2.分解质因数
100 = 2 * 2 * 5 * 5; 18 = 2 * 3 * 3;

import java.util.Scanner;
public class Exer{
    public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        int number = s.nextInt();
        while(number!=1){
            for(int i=2;i<=number;i++){
                if(number%i==0){
                    System.out.println(i);
                    numbber/=i;
                    break;
                }
            }
        }
    }
}

这部分练习比较多,有些我自己写的,不一定好,大家可以自己写写练习一下,有助于提高自己的能力。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值