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}不 ...
随机推荐
- clickhouse 安装部署(linux)
1.安装部署 1.1下载文件 可以按照官网步骤安装 https://clickhouse.tech/docs/zh/getting-started/install/. 这个库目前大小有2G,网络不允许 ...
- kettle连接mysql报Communications link failure
添加2个命名参数 1.autoReconnect=true 2.useSSL=false
- jxg项目Day4-数据库和mybatis的连接映射
配置:yml配置文件中配置数据库的参数,还有映射的参数 1.建实体类User,属性与数据库表对应 2.Mapper包下建UserMapper,继承BaseMapper<User> 3.Se ...
- LVS负载均衡 2022年4月
1. 负载均衡技术简介 2 1.1 负载均衡类型3 1.2 LVS简介4 1.3 Keepalived简介5 2. 负载均衡搭建主要步骤 6 2.1 LVS+Keepalived的负载均衡系统搭建6 ...
- 共享USB打印机设置方法
打印机共享 一.准备 所有计算机在同一个网段. 所有计算机在同一个工作组,组名可以自定义,默认WORKGROUP. 使用超级管理员用户,目的是为了激活guest用户.验证之后可以不需要此前提. 二.主 ...
- ASM1117脚位图
- 基于leaflet地图可视化(一)
最近,在学习地图可视化是基于公司的项目.但公司在项目上居然用图片来代替.无语~~~项目效果图(第一版)如下: 突发奇想,2016年自己就接触过地图可视化.但那是没有深入研究.只会用R语言来实现点基础. ...
- OD机试题-2022.4
import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.Scanne ...
- Linux下apache日志(按日期存放)分析与状态查看方法
转载网址: https://blog.csdn.net/weixin_42272246/article/details/125602258
- 内存取证 volatility的使用
volatility 简介: volatility(挖楼推了推) 是一个开源的框架,能够对导出的内存镜像进行分析,能够通过获取内核的数据结构,使用插件获取内存的详细情况和运行状态,同时可以直接dump ...