问题:求三个数中的最大值

上代码--

第一种  两两比较  每次取较大值 和第三个值比较  最终得到最大值

    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 怎么用?的更多相关文章

  1. 实验三——for 语句及分支结构else-if

    1.本节课学习到的知识点:在本次课中,我学习了for语句的使用,认识了for语句的执行流,明确了三种表达式的意义.以及最常用的实现多分支的else-if语句. 2.实验过程中遇到的问题及解决方法:在本 ...

  2. ecshop if标签,超过N条,就输出记录 elseif、库存显示方式

    <!--商品详情右侧 相关商品推荐--> <!-- {if $related_goods} --> <!--{foreach from=$related_goods it ...

  3. 作业3---for语句及分支结构else-if

    1.本次课学习到的知识点: (1)for语句的一般表达式,执行顺序: (2)指定次序的循环程序设计:数列的累加.累乘等: (3)else-if实现的分支结构可以判断语句的真假 2.实验过程中遇到的问题 ...

  4. freemarker if elseif

    FreeMarker模板 if, else, elseif 指令 : if, else, elseif 语法 <#if condition> ... <#elseif conditi ...

  5. 实验三--for语句及分支结构else-if

    本节课学习到的知识点: 1.for语句的表达式的应用与掌握.流程形式. 2.多分支else-if,用来判断真假等. 实验中遇到的问题及解决方法: 这次课的逻辑要求比之前的课要难许多,而且对于一些数学逻 ...

  6. 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 ...

  7. VB的if和elseif

    VB中if和elseif的用法是: if...then...elseif...then...else...endif 切记在then的后面不要加冒号,加了冒号出现else没有if的错误,因为加了冒号表 ...

  8. s标签可以if elseif else

    首先引用s标签: <%@ taglib prefix="s" uri="/struts-tags" %> 使用s标签进行if elseif else ...

  9. matlab中使用elseif和if嵌套的对比

    % 目标: % 判定成绩等级 %定义变量 % 输入:分数grade %清除变量或指令 clc; % 允许用户输入参数 disp ('该功能练习if语句'); disp ('输入你的成绩,系统将判定等级 ...

  10. freemarker中的if...elseif...else语句

    freemarker中的if...elseif...else语句 1.设计示例 <#if student.studentAge lt 12> ${student.studentName}不 ...

随机推荐

  1. Vue+SSM+Element-Ui实现前后端分离(2)

    前言:后台使用ssm搭建,对以前学习知识的一个回顾,与此同时来发现自己不足.这里主要采用配置文件方式进行,有部分注解. 目标:搭建ssm框架,并测试成功:(其中也有aop切面的编写) 一.开发工具 I ...

  2. 转载·Charles4.2.8 开启macOS Proxy ,MacOS10.15 Catalina版本提示APP权限为只读

    转载地址:https://superuser.com/questions/1490116/charles-4-2-8-cannot-configure-your-proxy-settings-whil ...

  3. constexpr 和常量表达式

    常量表达式(是const expression) 是指值不会改变并且在编译过程中就能得到计算结果的表达式.显然,字面值属于常量表达式,用常量 表达式初始化的const 对象也是常量表达式.后面将会提到 ...

  4. 10.7 2020 实验 5:OpenFlow 协议分析和 OpenDaylight 安装

    一.实验目的 回顾 JDK 安装配置,了解 OpenDaylight 控制的安装,以及 Mininet 如何连接:通过抓包获取 OpenFlow 协议,验证 OpenFlow 协议和版本,了解协议内容 ...

  5. ASP.NET在Repeater中使用Button控件报错

    普通Button在这里会报错,小编找了一天也没有解决这个问题, 这里可以换做LinkButton或者ImageButton替换普通的Button

  6. ansible介绍与简单的使用

    在roles下建立site.yml文件#site.yml - hosts: webservers remote_user: root roles: - websrvs - dbsrvs#将文件拷贝到f ...

  7. Qt中的渲染

    Qt中3种不同的渲染方式 1 )Qt::AA_UseDesktopOpenGL 使用显卡的openGL库,且要求支持openGL 2.1及以上的版本.因此很多老旧设备是不满足版本要求的(windows ...

  8. ArrayList学习

    核心源码 package java.util; import java.util.function.Consumer; import java.util.function.Predicate; imp ...

  9. Maven 切换JDK版本

    欢迎访问我的个人博客:xie-kang.com 查看Maven安装目录的conf目录可以看到有 settings.xml\toolchains.xml文件.settings.xml主要是设置切换Mav ...

  10. Tesseract5+OpenCV4(VS2017+win10)实现OCR识别

    一.环境配置 较之前采用cppan进行编译的方式,vcpkg的方式已经发生了许多变化,带来的最大不同就是便捷. 对于在NuGet中能够找到的Vcpkg的export,真的实现了开箱即用 这样的话对于普 ...