Java流程控制02:if选择结构

news/2024/10/19 1:37:03/

      本节内容教学视频链接:Java流程控制04:if选择结构_哔哩哔哩_bilibiliicon-default.png?t=N7T8https://www.bilibili.com/video/BV12J41137hu?p=36&vd_source=b5775c3a4ea16a5306db9c7c1c1486b5

   在Java中,if 选择结构用于根据特定条件执行不同的代码块。

        if语句有四种结构:if单选择结构、if双选择结构、if多选择结构、if嵌套结构;

1.if单选择结构(if)

代码示例1:单选择结构

java">import java.util.Scanner;public class IfDemo01 {public static void main(String[] args){Scanner scanner = new Scanner(System.in);System.out.println("请输入内容");String s = scanner.nextLine();if (s.equals("Hello")){System.out.println(s);}System.out.println("End");scanner.close();}
}

代码示例2:单选择结构

java">int number = 10;
if (number > 0) {System.out.println("Number is positive");
}

2.if双选择结构(if...else

代码示例1:

java">import java.util.Scanner;public class IfDemo02 {public static void main(String[] args) {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();}
}

代码示例2:

java">int number = -10;
if (number > 0) {System.out.println("Number is positive");
} else {System.out.println("Number is non-positive");
}

3.if多选择结构(if...else if...else

代码示例1:

java">import java.util.Scanner;public class IfDemo03 {public static void main(String[] args) {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("您的输入的成绩不合法");}}
}

代码示例2:

java">
int number = 25;
if (number <= 0) {System.out.println("Number is non-positive");
} else if (number < 10) {System.out.println("Number is between 1 and 10");
} else {System.out.println("Number is greater than 10");
}

3.if嵌套结构

java">
int x = 10;
int y = 20;
if (x > 0) {if (y > 0) {System.out.println("Both x and y are positive");} else {System.out.println("x is positive and y is non-positive");}
} else {if (y > 0) {System.out.println("x is non-positive and y is positive");} else {System.out.println("Both x and y are non-positive");}
}


http://www.ppmy.cn/news/1509403.html

相关文章

Armv9.4架构扩展-总结介绍

FEAT_ABLE:支持将监视点与地址匹配断点链接,适用于AArch64和AArch32状态,从Armv9.3起为可选特性。 FEAT_BWE:支持定义包含范围和排除范围的断点,适用于AArch64状态,从Armv9.3起为可选特性。 FEAT_CHK:引入CHKFEAT指令,用于检测特性是否启用,从Armv9.4起为必选特性,仅…

Django数据库一对多字段

3.16 Django数据库一对多 一对多关系通常通过外键&#xff08;ForeignKey&#xff09;来建立。 示例&#xff1a; 1&#xff09;创建Author和book models class class Auther(models.Model):namemodels.CharField(verbose_name作者,max_length32)class Book(models.Model):…

Unity教程(十)Tile Palette搭建平台关卡

Unity开发2D类银河恶魔城游戏学习笔记 Unity教程&#xff08;零&#xff09;Unity和VS的使用相关内容 Unity教程&#xff08;一&#xff09;开始学习状态机 Unity教程&#xff08;二&#xff09;角色移动的实现 Unity教程&#xff08;三&#xff09;角色跳跃的实现 Unity教程&…

Haproxy讲解

Haproxy: haproxy是一个开源的高性能反向代理和负载均衡器&#xff0c;主要用于‌TCP和‌HTTP流量管理。 功能和特点&#xff1a;haproxy能够处理大量的并发连接&#xff0c;支持TCP和HTTP协议&#xff0c;具有高可用性和负载均衡功能。它特别适用于需要处理大量流量的网站&am…

李沐老师动手深度学习pytorch版本的读取fashion_mnist数据并用AlexNet模型训练,其中修改为利用本地的数据集训练

李沐老师的d2l.load_data_fashion_mnist里面没有root参数&#xff0c;所以只会下载&#xff0c;不能利用本地的fashion_mnist数据。所以我使用torchvision 的datasets里面FashionMNIST方法&#xff0c;又由于李沐老师此处是利用AlexNet模型来训练fashion_mnist数据&#xff0c;…

【区块链教程】如何使用自动化脚本创建小狐狸地址

在很多场景下&#xff0c;不管是撸毛也好&#xff0c;批量操作也好&#xff0c;都需要使用到大量的qianbao地址。 如何一键创建&#xff0c;也成为了很多人想学的技术。 创建地址的自动化脚本可以极大地简化在区块链开发和测试中的管理流程。以下是一个基本的流程和示例脚本&am…

msgqueue.hpp队列模块

目录 一.MsgQueue模块介绍 二.MsgQueue类的实现 成员变量 构造函数与析构函数 成员函数 参数设置函数 setArgs 参数获取函数 getArgs 三.MsgQueueMapper类的实现 成员变量 构造函数 成员函数 创建表格函数 createTable 删除表格函数 dropTable 插入数据函数 inse…

Stable Diffusion绘画 | 提示词基础原理

提示词之间使用英文逗号“,”分割 例如&#xff1a;1girl,black long hair, sitting in office 提示词之间允许换行 但换行时&#xff0c;记得在结尾添加英文逗号“,”来进行区分 权重默认为1&#xff0c;越靠前权重越高 每个提示词自身的权重默认值为1&#xff0c;但越靠…