[JAVA] Java 中的选择结构(if、if-else、if-else if-else、switch-case)

基于Simulink使用激光雷达数据跟踪车辆仿真(附源码) 这些模型将长方体的尺寸定义为状态转换期间的常量,并且它们的估计值在滤波器的校正阶段随时间演变。下面的动画显示了在滤波器的预测阶段将具有不同概率的恒定速度和恒定转弯率模型混合在一起的效果。此示例显示了如何使用带有 IMM 过滤器的 JPDA 跟踪器来跟踪使用激光雷达传感器的对象。图像和激光雷达数据读取器从 MAT 文件中读取记录的数据,并分别输出参考图像和点云中点的位置。跟踪器块选中“启用所有轨道输出”和“启用可检测的跟踪 ID 输入”复选框,以从跟踪器输出所有轨道,并将其检测概率作为其状态的函数进行计算。 阅读详情

 

目录

1.单分支结构

-----1.1基本的if选择结构

-----1.2 多条件if选择结构

2.双分支结构 

3.多分支结构

-----3.1多重if选择结构

-----3.2 switch选择结构

4.嵌套条件结构 

 


程序化设计的基本结构 有程序结构、选择结构和循环结构三种。

其各自的流程图如下:

选择结构通过判断某些特定条件是否满足来决定下一步的执行流程,是非常重要的控制结构。

常见的有单分支选择结构、双分支选择结构、多分支选择结构以及嵌套的分支结构。

其形式灵活多变,具体使用哪一种最终还是取决于要实现的业务逻辑

1.单分支结构

单分支结构可以处理单一条件或者简单的组合条件选择情况。

-----1.1基本的if选择结构

语法:

if  (判断条件){

        主体语句

}

-----1.2 多条件if选择结构

可以用常见逻辑运算符来连接多条件

以下示例用逻辑运算符连接复杂条件:

2.双分支结构 

双分支结构用于处理非A即B的简单条件选择情况。

语法:

if (判断条件){

        代码块1;

}else{

        代码块2;

}

3.多分支结构

多分支结构用于处理复杂的条件分支情况,如果判断的条件是一个常量,可以用switch选择结构,使代码简洁易懂。

-----3.1多重if选择结构

语法:

if(判断条件1){

        代码块1;

}else if (判断条件2){

        代码块2;

}else{

        代码块3;

}

-----3.2 switch选择结构

语法:

switch(变量){

case 常量值1:

        代码块1;

        break;

case 常量值2:

        代码块2;

        break;

default:

        代码块3;

        break;

}

注意:

1.case后不能写变量的,必须是常量!

2. break 不能少!

switch的特性是 用变量匹配具体的case 后面的常量值,只要匹配到了某一个case,就会一直执行,遇到break才结束!

3.default 的作用是,如果匹配不到任何case,就执行default 后面的语句。

4.嵌套条件结构 

嵌套条件结构多用于一些复杂的流程控制。

 语法:

if (判断条件1){

        if (判断条件2){

        代码块1;

}else{

        代码块2;

}

}else{

        代码块3

}

 

【DRAM存储器四十七】LPDDR5介绍--LPDDR5的低功耗技术之DVFS 讲述LPDDR5的DVFSC和DVFSQ的工作原理和要求 阅读详情

相关推荐

从FrSky到TBS:深度解析航模2.4G遥控协议江湖史(附ELRS高频头改装指南)

本文深度解析了航模2.4G遥控协议的技术演进史,从FrSky ACCST的兴衰到TBS Crossfire的崛起,再到开源协议ExpressLRS的革命性突破。文章不仅对比了各协议在延迟、距离与抗干扰能力上的技术内核,还提供了为多协议遥控器加装TBS Crossfire高频头的实战改装指南,帮助玩家打通协议壁垒,提升飞行体验。

weixin_29193259的博客 751

分支结构:单分支if、多分支if-else、switch语句

java中的程序结构 1)顺序结构:编写的 每行 代码 一定 会执行 一次 2)分支结构:编写的代码 可能 会执行 一次 3)循环结构:编写的代码 可能 会执行 多次 分支结构 根据某个条件的真或假决定是否运行某段代码 1)单分支 if语句 if(<条件>){ //当条件为真时运行的代码 }//{}结束后无论条件是真是假,都继续执行外面的代码 {}可以省略,但是不写{}只对一行代码生...

liyu4352502的博客 1万+

【Matlab 传感器布局优化】基于群智能算法的wsn覆盖优化研究

本文主要是对同构和异构传感器在指定空间内的布局进行优化,以最大覆盖率为目标,使用了DBO/SCA/WOA/SSA/NOA等启发式算法,在100x100的空间内,对30,100,200个传感器进行优化,得到最优的布局位置和对应覆盖率。

u013367499的博客 1826

条件结构(分支结构)/循环结构

一.条件结构(分支结构) 1. if条件结构是根据条件判断之后再做处理 基本的if语法: if(条件){ //语句 } 2. if-else条件结构 if(条件){ //语句块1 }else{     //语句块2 } 3. 多重if结构   如果第一个条件不满足就会走入后面的,以此类推; 例如:对学员的结业考试成绩评测   成绩>=90:优秀   成绩>=80:

jffhy2017的博客 2472

本地部署llama3Chinese

这通常意味着你的构建环境可能不完整或者没有正确配置。通常是由 Visual Studio 的 C++ 工具提供的。(大模型对错误的解析)错误信息表明在尝试构建一个项目时,在 Windows 上,

m0_49343639的博客 1439

Shopee前端反爬三要素:x-sap-Ri、x-sap-Sec与af-ac-enc-dat深度解析

电商前端反爬机制本质是运行时环境可信验证,其核心在于动态生成的请求指纹、会话签名与行为加密载荷。这类机制不依赖静态Token,而是基于浏览器真实交互(如鼠标轨迹、页面可见性、时间精度)实时计算,体现JS运行时环境不可模拟性。技术价值在于将风控前移至客户端,大幅提升自动化绕过成本;典型应用场景包括商品价格监控、竞品数据采集与合规爬虫开发。本文聚焦Shopee实战案例,深入拆解x-sap-Ri(设备+行为哈希指纹)、x-sap-Sec(动态密钥签名)及af-ac-enc-dat(AES加密行为载荷)三大字段的生

weixin_29015127的博客 425

流程控制—分支结构if-else、switch循环语句while、do...while

流程控制 1顺序结构 顺序结构是程序中最简单的流程控制,没有特定的语法结构,按照代码的先后顺序依次执行。 2.分支结构 单分支 if语句 格式: if (关系表达式){ 语句体; } if-else双分支结构 if (){ } else { } if-else多分支结构 格式 if(关系表达式){ ; }else if (关系…){ ; }else{ } 3.switch语句 格式:(伪代码) switch(表达式){ case 值1: 语句体1; break; case 值2: 语句体2

m0_53179419的博客 392

【实战分享】低成本K型热电偶测温方案:单运放放大与STM32 ADC采集优化

本文详细介绍了低成本K型热电偶测温方案,通过单运放放大与STM32 ADC采集优化,解决了信号微弱、负电压处理和冷端补偿等核心挑战。方案采用LMV358运放和T型网络降低噪声,结合STM32的ADC校准和软件滤波,实现±0.5℃精度,物料成本控制在15元以内,适用于工业测温场景。

weixin_30839881的博客 177

【Java学习笔记】10.条件语句 - if...else及switch case 语句

本章介绍Java的条件语句 - if...else和switch case 语句。

qq_62052372的博客 1099

Android 13 录音APK在后台无数据的处理

未获取到录音数据

chenchen4310的博客 1186

42、使用mmrotate中k3det进行旋转目标检测,并进行mnn部署和ncnn部署

基本思想:仍然是身份证分割,并进行ncnn和mnn的c++的部署开发

sxj731533730 5723

switch case和if else对比

这种手动创建的跳转表与编译器生成的跳转表有些差异,但它演示了跳转表的基本思想。在实际编程中,编译器通常会根据情况自动进行类似的优化,而无需手动创建跳转表。语句中,使用跳转表的优化通常是由编译器来完成的,而不是手动编写跳转表的方式。跳转表是一个数组,数组的每个元素都是一个代码段的地址,这个代码段实现了当开关索引值等于该元素的索引时程序应该采取的动作。的值非常稀疏,那么使用数组进行简单映射就会有很多空的槽,于是就不适合生成查找表。的情况比较多(例如4个以上),并且值的范围跨度比较小时,就会使用跳转表。

我的博客 1659

Debezium报错处理系列之第102篇:解决Flink CDC报错The db history topic or its content is fully or partially missing

Debezium报错处理系列之第101篇:解决Flink CDC报错The db history topic or its content is fully or partially missing

zhengzaifeidelushang的博客 2272

(十一)java-【选择结构】+【if-else】+【多重if】+【switch-case】+【选择嵌套】

顺序结构选择(分支)结构循环结构。

qq_36352136的博客 2128

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

java选择结构if else switch case

if else public class HelloWorld { //变量是可以改变的 public static void main(String[] args) { //选择结构 if else //下雨,选择是否出去活动 boolean isRain = true; if(isRain) { System.out.println("天公不作美,在家吃鸡腿"); ...

weixin_45679977的博客 209

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

java中 if-else , switch-case 选择结构

一,if选择结构第一种:if(条件表达式){执行表达式}第二种:二选一if(条件表达式){执行表达式1}else {执行表达式}第三种:n选一if(条件表达式){执行表达式1}else if(条件表达式){执行表达式2}else if(条件表达式){执行表达式3}......................................else{执行表达式n}注:if(xxx.equals("....."))表示把.....赋值给xxx if-else结构可以相互嵌套,else结构是可选的

lhjava1234的博客 835

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Java选择结构详解:if-else与switch-case实战指南

流程控制是编程语言的核心概念,其中选择结构通过条件判断实现代码分支执行。在Java中,if-else和switch-case是最常用的选择结构,其底层通过字节码跳转指令实现。选择结构在业务逻辑处理、权限校验、异常处理等场景中具有重要价值,例如电商平台的会员折扣计算、用户登录验证等场景都依赖选择结构。合理使用if-else嵌套和switch-case穿透特性可以提升代码执行效率,而避免浮点数比较误差和空指针异常则是常见问题解决方案。随着Java版本更新,switch表达式等新特性进一步简化了多分支处理的代码编

weixin_29611239的博客 298

Java选择结构详解:if-else与switch-case语法、易错点及面试实战

在Java编程中,选择结构是控制程序流程的核心基础,通过条件判断让代码在多种路径中做出决策。掌握if-else与switch-case的语法差异、执行原理及常见陷阱,是写出健壮、可读代码的关键一环。从逻辑运算符的短路特性到switch的case穿透机制,再到JDK 12+的箭头新语法,深入理解这些细节不仅能够提升代码质量,还能有效应对Java基础面试中的高频考点。选择结构广泛应用于成绩分档、日期计算、状态流转等真实业务场景,配合闰年判断等经典案例,可以直观感受分支逻辑的工程价值。无论是Java初学者还是准备

weixin_33226548的博客 311

Java中if-else和switch-case

Java中if-else和switch-case 最简单易懂的讲解,三分钟学会if-else和switch-case

m0_60438688的博客 1244

Java-Day04 程序流程控制:使用Scanner类从键盘获取不同类型的变量、选择结构语句(if-else、switch-case)、循环结构语句(for、while、do while)

1. 使用Scanner类从键盘获取不同类型的变量;2. 选择结构2.1 if-else结构2.2 switch-case结构;3. 循环结构:3.1 for 循环,3.2 while循环,3.3 do while循环,3.4 嵌套循环;4. 特殊关键字的使用: break、continue,4.1 break关键字,4.2 continue关键字......

weixin_51612062的博客 655

选择结构的知识点

if单选择结构 import java.util.Scanner; public class IfDemo01 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入内容"); String str= sc.nextLine(); //equals判断字符串是否相等 if(str.equals("hello")){ System.out.printl

m0_59453939的博客 395

JAVA-04-选择结构

匹配 case标签,匹配后进入执行,break跳出。如果没有case匹配,会执行default。中的语句,直到碰到break为止。但这种情况并不安全。注意:如果匹配了其中一个,但是没有break,失效,会向下执行每一条。

LJH_laura_li的博客 282

JavaSE(基础篇)——选择结构(if 和 switch的使用)

无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。e 语句块有 break 语句则会跳出判断。【case穿透】**无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。

2401_85699170的博客 579

Java程序设计的基本结构 - 选择结构

文本关键字:基本结构、选择结构、if、else、switch、case。

微光点亮星辰 1万+

if 、switch、选择结构

我们很多时候需要判断一个东西是否可行,然后我们才去执行,这样一个过程在成程序中用if语句来表示语法if(布尔表达式){//如果布尔表达式为true将执行的语句。

ablinda的博客 495

java switch if语句_Java控制语句之if、switch语句

java if语句Java控制语句分为三大类:①顺序结构;②选择结构;③循环结构。--------------------------------------------------------------------------------选择结构又分为:①单选择结构;②双选择结构;③多选择结构。主要涉及: if_else , switch , while , break 和 continue ...

weixin_30086041的博客 180
上一篇: [EXCEL] 宏的录制、调用和删除
下一篇: [MySQL] MySQL 面试题集锦(1)
霞露
博客等级 码龄8年 602粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值