详解Java中的流程控制

 更新时间:2021年05月26日 11:08:02   作者:东北卖参哪些年  
今天带大家复习Java基础知识,文中对Java流程控制作了非常详细的介绍及代码示例,对正在学习Java的小伙伴们有很好地帮助,需要的朋友可以参考下

1.分支结构的概念

当需要进行条件判断并做出选择时,使用分支结构

2.if分支结构

格式:
if(条件表达式){
	语句块;
}
package com.lagou.Day04;

import java.util.Scanner;

/**
 * 编程使用if分支结构模拟网吧上网的过程
 */
public class Demo01 {
    public static void main(String[] args) {
        //1.提示用户输入年龄信息并使用变量记录
        System.out.println("请输入您的年龄:");
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();
        //2.使用if分支结构判断是否成年并给出对应的提示
        if (age>=18){
            //3.打印一句话
            System.out.println("开心的浏览起了网页...");
        }
        System.out.println("美好的时光总是短暂的!");
    }
}

3.if分支结构找最大值的方式一

package com.lagou.Day04;

import java.util.Scanner;

/**
 * 编程使用if分之结构查找两个整数中的最大值
 */
public class Demo02 {
    public static void main(String[] args) {
        //1.提示用户输入两个整数并使用变量记录
        System.out.println("请输入两个整数");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        //2.使用if分支结构找到最大值并打印
        if (a>=b){
            System.out.println("最大值"+a);
        }
        if (a<b){
            System.out.println("最大值"+b);
        }
    }
}

4.if分支结构查找最大值的方式二

package com.lagou.Day04;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        //1.提示用户输入两个整数并使用变量记录
        System.out.println("请输入两个整数");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        //方式二
        int max = a;
        if (b>max){
            max=b;
        }
        System.out.println("最大值是:"+max);
    }
}

5.ifelse分支结构的概念和使用

package com.lagou.Day04;

import java.util.Scanner;

/**
 * 编程使用ifelse分支结构来模拟考试成绩查询的过程
 */
public class Demo04 {
    public static void main(String[] args) {
        //1.提示用户输入考试成绩并使用变量记录
        System.out.println("请输入您的考试成绩:");
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();
        
        //2.使用if else分支结构判断考试成绩是否及格并给出对应的提示
        if (score >= 60){
            System.out.println("恭喜你考试通过了!");
        }else {
            System.out.println("下学期来补考吧!");
        }
    }
}

6.ifelse分支结构判断负数和非负数

提示用户输入一个整数,使用if else分支结构判断该整数是负数还是非负数并打印。

package com.lagou.Day04;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        System.out.println("请输入一个整数");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        if (num<0){
            System.out.println(num+"是负数");
        }else {
            System.out.println(num+"是非负数");
        }
    }
}
  • 使用if else分支结构判断该整数是正数、负数还是零
package com.lagou.Day04;

import java.util.Scanner;

public class Demo06 {
    public static void main(String[] args) {
        System.out.println("请输入一个整数");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        
        if (num<0){
            System.out.println(num+"是负数");
        }else {
            if (num>0){
                System.out.println(num+"是正数");
            }else {
                System.out.println(num+"是零");
            }
        }
    }
}

7.if else if else分支结构的概念和使用

结构
if(条件表达式1){
	语句块1;
}else if(条件表达式2){
	语句块2;
}else{
	语句块n;
}
package com.lagou.Day04;

import java.util.Scanner;

public class Demo07 {
    public static void main(String[] args) {
        System.out.println("请输入身份信息");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        
        if ("军人".equals(str)){
            System.out.println("免费乘车");
        }else if ("学生".equals(str)){
            System.out.println("请购买半价票");
        }else {
            System.out.println("请购买全价票");
        }
    }
}

8.个人所得税的计算方式一

package com.lagou.Day04;

import java.util.Scanner;

public class Demo08 {
    public static void main(String[] args) {
        System.out.println("请输入个人薪水");
        Scanner sc = new Scanner(System.in);
        int salary = sc.nextInt();
        double salaryPrice = 0.0;
        if (salary<=5000){
            System.out.println("无需纳税");
        }else if (salary<=8000){
            salaryPrice = (salary-5000)*0.03;
        }else if (salary <= 17000){
            salaryPrice = (salary-8000)*0.1+(8000-5000)*0.03;
        }else if (salary <= 30000){
            salaryPrice = (salary-17000)*0.2+(17000-8000)*0.1+(8000-5000)*0.03;
        }
        System.out.println(salaryPrice);
    }
}

9.个人所得税的计算方式二

package com.lagou.Day04;

import java.util.Scanner;

public class Demo09 {
    public static void main(String[] args) {
        System.out.println("请输入你的薪水");
        Scanner sc = new Scanner(System.in);
        int salary = sc.nextInt();
        double salaryPrice = 0.0;
        if (salary<=5000){
            System.out.println("无需纳税");
        }else if (salary <= 8000){
            salaryPrice = (salary-5000)*0.03 -0;
        }else if (salary<=17000){
            salaryPrice=(salary-5000)*0.1-210;
        }else if (salary<=30000){
            salaryPrice=(salary-5000)*0.2-1410;
        }
        System.out.println(salaryPrice);
    }
}

10.if分支结构实现等级判断

package com.lagou.Day04;

import java.util.Scanner;

public class Demo10 {
    public static void main(String[] args) {
        System.out.println("请输入考试成绩");
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();
        if (score >= 90 && score <= 100){
            System.out.println("等级A");
        }else if (score >= 80){
            System.out.println("等级B");
        }else if (score >= 70){
            System.out.println("等级C");
        }else if (score >= 60){
            System.out.println("等级D");
        }else {
            System.out.println("等级E");
        }
    }
}

11.switch case分支结构概念

12.switch case代码

package com.lagou.Day04;

import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) {
        System.out.println("请输入你的成绩");
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();

        switch (score / 10){
            case 10:
                System.out.println("等级A");
                break;
            case 9:
                System.out.println("等级A");
                break;
            case 8:
                System.out.println("等级B");
                break;
            case 7:
                System.out.println("等级C");
                break;
            default:
                System.out.println("等级D");    
        }
    }
}
  • switch()中支持的数据类型有:byte、short、char以及int类型,jdk1.5开始支持枚举类型,从jdk1.7开始支持String类型

13.switch case分支结构实现字符界面

package com.lagou.Day04;

import java.util.Scanner;

/**
 * 模拟菜单的效果
 */
public class Demo12 {
    public static void main(String[] args) {
        //1.绘制字符界面
        System.out.println("        欢迎来到lg教育         ");
        System.out.println("-----------------------------");
        System.out.print("[1]学员系统       ");
        System.out.print("[2]管理员系统");
        System.out.println("[0]退出系统");
        System.out.println("------------------------------");
        System.out.println("请选择要进入的系统");
        Scanner sc = new Scanner(System.in);
        int choose = sc.nextInt();

        switch (choose){
            case 1:
                System.out.println("正在进入学员系统");break;
            case 2:
                System.out.println("正在进入管理员系统");break;
            case 0:
                System.out.println("谢谢使用,下次再见!");
            default:
                System.out.println("输入错误,请重新选择!");
        }
    }
}

14.循环结构

  • 在Java程序中若希望重复执行一段代码时,就需要使用循环结构
  • 任何复杂的程序逻辑都可以通过顺序、分支、循环三种程序结构实现。

15.for循环

for(初始化表达式;条件表达式;修改初始值表达式){ ​ 循环体; }

package com.lagou.Day04;

public class Demo13 {
    public static void main(String[] args) {
        for (int i = 1;i<=10;i++){
            System.out.println("大吉大利,今晚吃鸡"+"第"+i+"场");
        }
    }
}

16.for打印奇数

package com.lagou.Day04;

/**
 * 打印奇数 方式一
 */
public class Demo14 {
    public static void main(String[] args) {
        for (int i = 1;i<=100;i++){
            if ((i%2)!=0)
            System.out.println(i);
        }
    }
}
package com.lagou.Day04;

/**
 * 方式二
 */
public class Demo15 {
    public static void main(String[] args) {
        for (int i = 1;i<=100;i+=2){
            System.out.println(i);
        }
    }
}
package com.lagou.Day04;

/**
 * 方式三
 */
public class Demo16 {
    public static void main(String[] args) {
        for (int i = 1;i<=50;i++){
            System.out.println(2*i-1);
        }
    }
}

到此这篇关于详解Java中的流程控制的文章就介绍到这了,更多相关Java流程控制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • IntelliJ IDEA里找不到javax.servlet的jar包的解决方法

    IntelliJ IDEA里找不到javax.servlet的jar包的解决方法

    这篇文章主要介绍了IntelliJ IDEA里找不到javax.servlet的jar包的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Java 遍历取出Map集合key-value数据的4种方法

    Java 遍历取出Map集合key-value数据的4种方法

    这篇文章主要介绍了Java 遍历取出Map集合key-value数据的4种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • SpringBoot应用jar包启动原理详解

    SpringBoot应用jar包启动原理详解

    本文主要介绍了SpringBoot应用jar包启动原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03
  • 去掉IntelliJ IDEA 中 mybatis 对应的 xml 文件警告的教程图解

    去掉IntelliJ IDEA 中 mybatis 对应的 xml 文件警告的教程图解

    本文通过图文并茂的形式给大家介绍了去掉IntelliJ IDEA 中 mybatis 对应的 xml 文件警告的教程,需要的朋友可以参考下
    2018-06-06
  • JAVA正则表达式过滤文件的实现方法

    JAVA正则表达式过滤文件的实现方法

    这篇文章主要介绍了JAVA正则表达式过滤文件的实现方法的相关资料,希望通过本文大家能够掌握理解这部分内容,需要的朋友可以参考下
    2017-09-09
  • Mybatis-plus常见的坑@TableField不生效问题

    Mybatis-plus常见的坑@TableField不生效问题

    这篇文章主要介绍了Mybatis-plus常见的坑@TableField不生效问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Spring Boot Admin实现服务健康预警功能

    Spring Boot Admin实现服务健康预警功能

    这篇文章主要介绍了Spring Boot Admin实现服务健康预警功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot 微信退款功能的示例代码

    SpringBoot 微信退款功能的示例代码

    这篇文章主要介绍了SpringBoot 微信退款功能的实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 深入理解Java中Filter的作用种类及应用场景

    深入理解Java中Filter的作用种类及应用场景

    Filter(过滤器)是Java Web中的一种重要组件,可以对请求和响应进行拦截处理,对数据进行过滤和处理。Filter可以实现许多功能,如:鉴权、日志记录、字符编码转换、数据压缩、请求重定向等等
    2023-04-04
  • SpringCloud之Zuul网关原理及其配置讲解

    SpringCloud之Zuul网关原理及其配置讲解

    这篇文章主要介绍了SpringCloud之Zuul网关原理及其配置讲解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03

最新评论