首页 > Java > java教程 > 正文

如果还有其他嵌套,如果在Java中:

聖光之護
发布: 2025-01-31 08:06:01
转载
1052人浏览过

> if语句:
如果某个条件为true,则执行一系列语句。

>

image description

语法:>

if(条件){

//要执行的代码
> }

program:
public class if {  
public static void main(string[] args) {  
      float a =2.2f; 
      float b =5.4f; 
        if(b>a)
         {  
          system.out.println(" b is greater than a");  
           }  
       }  
}  


output:
b is greater than a
登录后复制

> if-else语句: 在java中,if-else条件是一组规则或语句,如果条件为真,则执行一个代码块,如果条件为false,则执行另一个块。

image description

语法:> if(条件){

//代码如果条件为true

} else {
//代码如果条件为false
}

**program**:

public class ifelse {

    public static void main(string[] args) {

        int n = 3;

        if (n > 5) {
            system.out.println("the number is greater than 5.");
        } 
         else  
        {
            system.out.println("the number is 5 or less.");
        }
    }
}


output:
the number is 5 or less.
登录后复制

else if语句:

如果第一个条件为false,则使用其他if语句使用其他if语句指定新条件。

>语法:

if(条件1){image description //如果条件1为true

,则要执行的代码块 } else if(condition2){

> //如果条件1为false,并且条件2为true
>,则要执行的代码块 } else {
//如果条件1为false,并且条件2为false
,则要执行的代码块 }

**program:**

public class greater
{
public static void main (string args[])
{
int no1 = 1000; 
        int no2 = 1000; 
        if(no1>no2)
          {
            system.out.println("no1 is greater");
          }
        else if(no2>no1)
         {
              system.out.println("no2 is greater");
         }      
        else 
           {
         system.out.println("both are equal");
           }
}
}

output:
     both are equal

登录后复制
如果在java

中,则嵌套 指另一个在另一个if语句中有一个if语句。如果外部条件为真,则将检查和执行内部条件。

如果 *

image descriptionif(condition1){

if(condition2){

if(condition3){ //语句; }

}

}

program:

    public class nested if
    {
    public static void main(string args[])
    {
        int a = 10;
        int b = 20;

        // outer if condition
        if (a == 10) {

            // inner if condition
            if (b != 20) {
                system.out.println("geeksforgeeks");
            }

            else {
                system.out.println("gfg");
            }
        }
    }
}


登录后复制


任务:
程序:1

爪哇

public class age
{
public static void main (string args[])
{
 int age1=25;
 int age2=21;
    if(age1<age2)
{
       system.out.println("the condition is false");
}
    else 
{
       system.out.println("the condition is true");
}
}
}

output:
 the condition is true

登录后复制

python

age1 = 25
age2 = 21

if age1 < age2:
    print("the condition is false")
elif age2 < age1:
    print("the condition is true")

output:
the condition is true
登录后复制
爪哇

program:2

public class if {  
public static void main(string[] args) {  
      float a =2.2f; 
      float b =5.4f; 
        if(b>a)
         {  
          system.out.println(" b is greater than a");  
           }  
       }  
}  

output:
b is greater than a

登录后复制

python

a = 2.2
      b = 5.4
if b > a:
    print("b is greater than a")

output:
  num is greater than or equal 120

登录后复制

爪哇

program:3

public class greater
{
public static void main (string args[])
{
int no1 = 1000; 
        int no2 = 1000; 
        if(no1>no2)
          {
        system.out.println("no1 is greater");
          }
        else if(no2>no1)
         {
           system.out.println("no2 is greater");
         }      
        else 
           {
      system.out.println("both are equal");
           }
}
}

output:
both are equal

登录后复制

python

no1 = 1000
no2 = 1000

if no1 > no2:
    print("no1 is greater")
elif no2 > no1:
    print("no2 is greater")
else:
    print("Both are equal")

output:
Both are equal

登录后复制

>参考:

参考:

参考:

https://www.javatpoint.com/java-if-else

以上就是如果还有其他嵌套,如果在Java中:的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号