软件编程
位置:首页>> 软件编程>> java编程>> 详解Java中的流程控制

详解Java中的流程控制

作者:东北卖参哪些年  发布时间:2022-03-29 06:52:19 

标签: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);
       }
   }
}

来源:https://blog.csdn.net/weixin_41147429/article/details/117235126

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com