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}不 ...
随机推荐
- 3vue
阻止冒泡 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- linux查看IP地址
方法一:ifconfig -a 方法二:ip addr
- linux软件安装篇
nginx篇 第一件事情 cd /etc/yum.repo.d mv CentOS-Base.repo CentOS-Base.repo.bak wget -O CentOS-Base.repo ht ...
- 4.3Dsmax捕捉工具
平面视图: Line 线 多用于平面视图(按住shift可创建水平或垂直的线) Cirde 圆 Arc 弧形 NGon 多边形 Text 文本 输入中文会出现方框,换一下字体即可 Egg 鹅卵形 re ...
- mysql-开启日志记录功能
开启日志记录功能 -- 开启功能 SET GLOBAL general_log = ON; -- 保存到文件 SET GLOBAL log_output = 'file'; 查看日志内容 -- 查看日 ...
- 关于TCP协议传文件的例子
按照惯例,先来进行复习,这也是自学巩固的一个过程 首先是在工程文件PRO里,需要增加network,这个是引用TCP监听套接字和连接套接字的前提 第二部,在服务端头文件server.h进行基础的配置: ...
- xcodeproj Building for iOS, but the linked and embedded framework ‘xxx.framework’ was built for iOS + iOS Simulator.
一.报错 报错内容大致如下 /xxxx/xxx.xcodeproj Building for iOS, but the linked and embedded framework 'xxx.frame ...
- Svn安装客户端鼠标右键报错SendRpt.exe not found
kill 掉 重启资源管理器就好了
- JsonResult向前端返回值,报错500
1,问题原因 因为返回信息为json对象,我在controller方法所在的入口类上,添加的注解是:@Controller 而@Controller是不适合返回json内容的 2,解决方法 方法一:不 ...
- JavaWeb笔记第一弹
一.MYSQL的安装 1.MYSQL的安装 可以去官网找到与自身计算机向对应的版本进行下载 网址如下: MySQL :: Download MySQL Community Server 2.MYSQL ...