详解Java中的流程控制
作者:东北卖参哪些年 时间:2022-03-29 06:52:19
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
标签:Java,流程控制,循环语句
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot如何重定向外部网页
2022-11-12 05:19:19
Java集合类中文介绍
2022-07-22 18:02:20
![](https://img.aspxhome.com/file/2023/6/62536_0s.jpg)
Spring Boot 2.X 快速集成单元测试解析
2023-11-11 02:45:05
![](https://img.aspxhome.com/file/2023/3/61503_0s.jpg)
java常用工具类之DES和Base64加密解密类
2023-12-20 17:48:31
为什么阿里要慎重使用ArrayList中的subList方法
2023-12-07 19:03:34
![](https://img.aspxhome.com/file/2023/0/82380_0s.jpg)
Java利用反射对list对象做过滤
2023-12-23 07:39:17
![](https://img.aspxhome.com/file/2023/7/63347_0s.png)
Java编程实现轨迹压缩算法开放窗口实例代码
2021-12-03 09:54:07
WPF实现动画效果(七)之演示图板
2021-08-18 20:31:53
浅谈java中静态方法的重写问题详解
2022-12-24 10:13:04
一文搞懂Spring循环依赖的原理
2023-07-24 19:03:24
java实现钉钉机器人消息推送的示例代码
2023-05-18 13:53:25
![](https://img.aspxhome.com/file/2023/5/82005_0s.png)
Android应用内悬浮窗Activity的简单实现
2023-07-28 04:03:00
![](https://img.aspxhome.com/file/2023/4/84704_0s.png)
C#实现winform中RichTextBox在指定光标位置插入图片的方法
2022-04-04 01:50:16
使用JAVA实现邮件发送功能的图文教程
2021-09-04 17:29:36
![](https://img.aspxhome.com/file/2023/9/71949_0s.png)
C#使用struct直接转换下位机数据的示例代码
2023-06-07 01:49:01
Android实现双层ViewPager嵌套
2021-12-23 02:35:52
![](https://img.aspxhome.com/file/2023/4/96804_0s.gif)
SpringBoot @Cacheable自定义KeyGenerator方式
2022-12-25 13:23:11
![](https://img.aspxhome.com/file/2023/9/88069_0s.png)
SpringMVC统一异常处理实例代码
2021-05-31 02:29:26
java复制文件的4种方式及拷贝文件到另一个目录下的实例代码
2023-05-15 16:03:25
java swing GUI窗口美化方式
2023-04-03 00:13:56
![](https://img.aspxhome.com/file/2023/3/90963_0s.jpg)