if elseif else 怎么用?
问题:求三个数中的最大值
上代码--
第一种 两两比较 每次取较大值 和第三个值比较 最终得到最大值

private static void maxIf2() {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
int c = (int) (Math.random() * 100);
int max = a;
if (max < b) {
max = b;
}
if (max < c) {
max = c;
}
System.out.println(a + "," + b + "," + c + "中最大值是:" + max);
}
if 实现
假设 a最大给max
让max 和 b 比较 取较大值给max
然后再让 max和c 比较 再取 较大值给 max
至此 max 和所以数据 比较完毕 为最大值
去掉额外变量

private static void maxIf4() {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
int c = (int) (Math.random() * 100);
System.out.print(a + "," + b + "," + c + "中最大值是:");
if (a < b) {
a = b;
}
if (a < c) {
a = c;
}
System.out.println(a);
}
if 没有max
该方式在a 不是最大值时 原来的值 将会被改变
第二种

private static void maxIf5() {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
int c = (int) (Math.random() * 100);
System.out.print(a + "," + b + "," + c + "中最大值是:");
int max =0;
if (a >b && a>c) {
max=a;
} else if ( b > c && b >a) {
max = b;
}else {
max=c;
}
System.out.println(max);
}
if else if
这中方式需要 把条件写的很复杂
if else if 是只执行满足条件的那一个 其余的不执行
问题:根据分数判断优良中差

public class IfElse {
public static void main(String[] args) {
// >=90 优 80<=score<90 良 60<= score <80 中 score<60 差
int score=95;
if(score <60){
System.out.println("差");
}else if(score <80){
System.out.println("中");
}else if (score <90){
System.out.println("良");
}else if(score>=90){ //该方式 最后一个条件 可以不写 不满足前面 score<90 else 就是 score>=90
System.out.println("优秀");
}
//错误示例
if(score <60){
System.out.println("差");
}else if(score >=60){
System.out.println("中");
}else if (score >=80){
System.out.println("良");
}else if(score >=90){
System.out.println("优秀");
}
}
}
if else 条件规律
在else 之后的if 是对上一条 if 相对立条件 的再细分
else if(score >=60){
System.out.println("中");
}else if (score >=80){
System.out.println("良");
}
这 score >80 和 上一个条件的对立条件= score<60 相矛盾 永远都不会被执行到
在正确的示例中
我们可以得到这么一个规律 整个if else 用统一的 > 或 <
如果第一if个用 >(≥)号 之后的值 if else 越多 参数值就该越小
如果第一if个用 <(≤)号 之后的值 if else 越多 参数值就该越大
if elseif else 怎么用?的更多相关文章
- 实验三——for 语句及分支结构else-if
1.本节课学习到的知识点:在本次课中,我学习了for语句的使用,认识了for语句的执行流,明确了三种表达式的意义.以及最常用的实现多分支的else-if语句. 2.实验过程中遇到的问题及解决方法:在本 ...
- ecshop if标签,超过N条,就输出记录 elseif、库存显示方式
<!--商品详情右侧 相关商品推荐--> <!-- {if $related_goods} --> <!--{foreach from=$related_goods it ...
- 作业3---for语句及分支结构else-if
1.本次课学习到的知识点: (1)for语句的一般表达式,执行顺序: (2)指定次序的循环程序设计:数列的累加.累乘等: (3)else-if实现的分支结构可以判断语句的真假 2.实验过程中遇到的问题 ...
- freemarker if elseif
FreeMarker模板 if, else, elseif 指令 : if, else, elseif 语法 <#if condition> ... <#elseif conditi ...
- 实验三--for语句及分支结构else-if
本节课学习到的知识点: 1.for语句的表达式的应用与掌握.流程形式. 2.多分支else-if,用来判断真假等. 实验中遇到的问题及解决方法: 这次课的逻辑要求比之前的课要难许多,而且对于一些数学逻 ...
- MySQL PLSQL Demo - 005.IF THEN ELSEIF THEN ELSE END IF
drop procedure if exists p_hello_world; create procedure p_hello_world(in v_id int) begin ) then sel ...
- VB的if和elseif
VB中if和elseif的用法是: if...then...elseif...then...else...endif 切记在then的后面不要加冒号,加了冒号出现else没有if的错误,因为加了冒号表 ...
- s标签可以if elseif else
首先引用s标签: <%@ taglib prefix="s" uri="/struts-tags" %> 使用s标签进行if elseif else ...
- matlab中使用elseif和if嵌套的对比
% 目标: % 判定成绩等级 %定义变量 % 输入:分数grade %清除变量或指令 clc; % 允许用户输入参数 disp ('该功能练习if语句'); disp ('输入你的成绩,系统将判定等级 ...
- freemarker中的if...elseif...else语句
freemarker中的if...elseif...else语句 1.设计示例 <#if student.studentAge lt 12> ${student.studentName}不 ...
随机推荐
- linux下opencv contrib安装
opencv安装 1.1 安装依赖 sudo apt-get update sudo apt-get install build-essential sudo apt-get install cmak ...
- JavaScript基础学习之一
目录 let和var之间的区别 作用域不同 变量提升 暂时性死区(temporal dead zone,简称 TDZ) 相同作用域下的重复声明 脚本调用 数据类型 Boolean Object 对象 ...
- This will upgrade your R installation.
sudo add-apt-repository ppa:marutter/rrutter sudo apt update sudo apt full-upgrade
- 前端复习之css
1.css概述 1 1.CSS3概述 2 1.问题 3 1.设置页面中所有的文本颜色为红色 4 2.设置页面中所有div的文本的颜色为蓝色 5 3.将所有的div的文本的颜色改为黄色 6 7 HTML ...
- abp框架+mysql 数据库 执行批量新增和修改
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken. ...
- Qt中的渲染
Qt中3种不同的渲染方式 1 )Qt::AA_UseDesktopOpenGL 使用显卡的openGL库,且要求支持openGL 2.1及以上的版本.因此很多老旧设备是不满足版本要求的(windows ...
- SQLServer游标(Cursor)简单例子
DECLARE @username nvarchar(50),@password nvarchar(50),@num int--声明游标变量 DECLARE myCursor CURSOR FOR s ...
- c#调用键盘输入
[code]csharpcode: /// <summary> /// 键盘输入模拟 /// </summary> [DllImport("user32.dll&qu ...
- 发布jar包到远程仓库 (maven deploy)
背景: 项目有开放服务模块,现有个需求,需要把开放服务提供成一个jar包,用户可以直接对接. 流程: 1.在pom.xml文件添加distributionManagement节点,将项目打包上传到私服 ...
- windos 环境下载安装seata
参考: https://blog.csdn.net/lianghecai52171314/article/details/127330916