详解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
投稿
猜你喜欢
- 目录线程同步的几种方法:阻塞加锁(lock)Monitors互斥锁(Mutex)信号和句柄InterlockedReaderWriterLo
- 1.vs中生成dll对应的生成dll的cpp如下 #include<opencv2/opencv.hpp>#inclu
- 一、引言“为什么我们需要掌握互操作技术的呢?” 对于这个问题的解释就是—&
- 一、前言1、热更新代码的场景(1)当线上服务器出现问题时,有些时候现有的手段不足以发现问题所在,可能需要追加打印日志或者增加一些调试代码,如
- Java中throws和throw的区别讲解当然,你需要明白异常在Java中式以一个对象来看待。并且所有系统定义的编译和运行异常都可以由系统
- 本文实例为大家分享了java金额数字转中文工具类的具体代码,供大家参考,具体内容如下java金额数字转中文工具类ConvertNum.jav
- 1、JavaBean介绍 * JavaBean的定义:JavaBeans是Java中一种特殊的类,可以将多个对象封装到一个对象(bean)
- 1.情景展示java发送get请求、post请求(form表单、json数据)至另一服务器;可设置HTTP请求头部信息,可以接收服务器返回c
- 本文实例讲述了Java日期操作方法工具类。分享给大家供大家参考,具体如下:package com.gcloud.common;import
- 本篇文章基于redisson-3.17.6版本源码进行分析一、主从redis架构中分布式锁存在的问题1、线程A从主redis中请求一个分布式
- 前言在第一篇文字中,我们完全人工方式,一个命令一个命令输入,实现一个java tomcat运行环境,虽然也初见成效,但很累人。如果依靠依靠脚
- 前言相对来说呢,jpg格式的相对来说容易破解一点,当然也取决于你的干扰元素,元素越复杂,破解也就难度越高,有的加的多,人都识别不出来了,何况
- 详解java中接口与抽象类的区别1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系。但是
- 要说this和super就不得不说Java的封装和继承了,首先说封装,这是一种思想,算不上一种技术,核心思想就是将对象的同一行为和状态看成是
- 相信大家在小的时候都玩过拼图游戏,现如今,手机普及,能在手机上玩的游戏越来越多,于是乎,重温小时候,编写这个简易拼图游戏,而且也能进一步加深
- 出处:https://www.cnblogs.com/SunSpring作为初学者配置maven一般网上搜索。然后你就看到各种配置文件片段,
- 基本概念Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制并发访问
- 已经自学OpencvSharp一段时间了(目前工作用的是C#,就学了Opencvsharp了,vs2015,opencvsharp3),收获
- 前言Condition是在Spring4.0增加的条件判断功能,通过这个功能可以实现选择性的创建Bean对象。引入一个例子SpringBoo
- 不依赖任何外界包,maven如何生成可以执行的jar?pom中不包含任何引用的情况下,只需要在pom中添加 maven-jar-plugin