java流程控制(重点)

12 篇文章 0 订阅
9 篇文章 0 订阅

java流程控制

结构有

  1. 顺序结构
  2. 选择结构
  3. 循环结构

选择结构

if

image-20210803030857316.png

package struct;
import java.util.Scanner;

public class dome1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容");
        String s = scanner.nextLine();
        //equals:判断字符串是否相等
        if (s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();

    }
}
if else

image-20210803030857316.png

package struct;

import java.util.Scanner;

public class dome2 {
    public static void main(String[] args) {
        //考试分数大于60及格,小于60不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score >= 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}
if ,else if,

image-20210802040050747.png

package struct;

import java.util.Scanner;

public class dome3 {

    public static void main(String[] args) {

        /**
         * if 语句至少有一个else语句,else语句在所有else if 之后
         * 一旦其中有一个else if 语句为true ,其他else if及else语句都将跳过执行
         */

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score==100){
            System.out.println("满分");
        }else if (score<100 && score>=90){
            System.out.println("A");
        }else if (score<90 && score>=80){
            System.out.println("B");
        }else if (score<80 && score>=70){
            System.out.println("C");
        }else if (score<70 && score>=60){
            System.out.println("D");
        }else if (score<60 && score>=0){
            System.out.println("不及格");
        }else {
            System.out.println("成绩不合法");
        }
        scanner.close();
    }


}
switch,break,default

image-20210802040233188.png

package struct;

public class dome4 {
    public static void main(String[] args) {
        //case穿透 //switch匹配一个具体值

        char grade = 'A';
        switch (grade) {
            case 'A':
                System.out.println("优秀");
                break;
            case 'b':
                System.out.println("良");
                break;
            case 'c':
                System.out.println("及格");
                break;
            case 'd':
                System.out.println("不及格");
                break;
            default:
                System.out.println("未知");
        }
    }

}
String

image-20210802034432411.png

循环结构

while

image-20210803014632345.png

package struct;

public class dome6 {
    public static void main(String[] args) {
        //输出1~100
        int i = 0;
        while (i < 100) {
            i++;
            System.out.println(i);
        }

    }
}
package struct;

public class whlieDome {
    public static void main(String[] args) {
        //计算1+2+3...100
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum + i;
            i++;

        }
        System.out.println("100以内和为;"+sum);//5050

    }
}
do while

image-20210803014713720.png

package struct;

public class dowhlie {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum + i;
            i++;

        } while (i <= 100);
        System.out.println(sum);

    }
}
for循环

image-20210803015015383.png

1.基础语法
package struct;

public class fordome {
    public static void main(String[] args) {
        int a = 1;//初始化条件
        while (a<=100){//条件
            System.out.println(a);//循环体
            a+=2;//迭代
        }
        System.out.println("while循环结束");
        //初始化 //条件 //迭代
        for (int i = 1; i <= 100; i = i+2) {
            System.out.println(i);

        }
        System.out.println("for循环结束");
    }
}
2.练习1
package struct;

public class fordome1 {
    public static void main(String[] args) {
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i%2!=0){
                oddSum+=i;
            }else {
                evenSum+=i;
            }
        }

        System.out.println("奇数和:"+oddSum);//2500
        System.out.println("偶数和:"+evenSum);//2550


    }
}
3.练习2
package struct;

public class fordome2 {
    public static void main(String[] args) {
        //for循环输出1-1000之间能被5整除的,并且每行输出3个
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){//每行3个
                System.out.println();
                //System.out.print("\n");
                //println 输出完换行
                //print 输出完不换行

            }
        }
    }
}
4.练习3
package struct;

public class fordome3 {
    public static void main(String[] args) {
        //99乘法表
        //打印第一列
        //把固定的1再用循环包起来
        //去掉重复值 i<=j
        //调整样式
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i) + "\t");
            }
            System.out.println();

        }
    }
}

Break及Continue

1.基础

image-20210803015825457.png

2.Break
package struct;

public class BreakDome {
    public static void main(String[] args) {
        for (int i = 1; i < 100; i++) {
            System.out.println(i);
            if (i==30){
                break;
            }
        }
        System.out.println("结束");
    }
}
3.Continue
package struct;

public class ContinueDome {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            if (i % 10 == 0) {
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
    }
}

练习及Debug

1.练习
package struct;


public class Exercise {
    public static void main(String[] args) {
        //打印三角形 5
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }


    }

}

     *
    ***
   *****
  *******
 *********
2.Debug

image-20210803020336498.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值