首页  >  文章  >  Java  >  Java 中的 Else-If 语句

Java 中的 Else-If 语句

王林
王林原创
2024-08-30 15:23:17978浏览

用于检查代码块是否要执行的条件语句称为 else-if 语句。如果指定的条件为真,则执行或执行代码的 else 块中给出的条件。该块代码用于测试条件是否为真,以便执行以下代码。 Else 语句块是可选的。此外,还有 if-else-if 语句和嵌套 if 语句。 if 条件只能使用 else。这是任何编程语言中的基本语句之一。

语法

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Else If 语句通常使用的语法就像一个梯子,如果一条语句不执行,则执行另一条语句。如果多次检查没有执行所有Else If语句,则最终执行Else语句,给出特定的输出。 Else If 语句的语法如下:

代码:

if(condition1)
{
//Specific code to be run if the Condition 1 is true according to the program.
}
else if(condition2)
{
// Specific code to be run if the Condition 2 is true according to the program
}
else if(condition3)
{
// Specific code to be run if the Condition 3 is true according to the program
}
...
else
{
// Specific code to be run if the Condition n is true according to the program false
}

在上面的语法中,我们注意到如果没有一个条件被执行,那么最后的Else语句就会被执行,这是第n个条件。语法与 If 语句极其相似。不同的是Else If语句中有多个If。

Java 中 Else-If 语句的流程图

Else If 语句的流程图与 If 语句非常相似。我们可以用流程图检查 Else If 语句的工作情况。如图所示,如果条件1为假,则执行条件2。如果也为 false,则执行条件 3,依此类推。
另一方面,如果条件 1 为真,则执行语句 1。另外,如果条件 1 为假,则转移到条件 2,如果条件 2 为真,则执行语句 2。

Java 中的 Else-If 语句

Java 中 Else-If 语句的示例

以下是下面提到的 Java 中 Else-If 语句的示例

示例#1

在第一个编码示例中,我们将输入一个数字并检查它是正数、负数还是零。在这种情况下,我们使用了 Else if 梯子并检查数字的行为。这是一个非常基本的程序,寻找数字的性质。

代码:

import java.io.*;
public class PositiveNegativeExample
{
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int n= Integer.parseInt(br.readLine());
if(n>0)
{
System.out.println("The number is POSITIVE");
}
else if(n<0)
{
System.out.println("The number is NEGATIVE");
}
else
{
System.out.println("The number is ZERO");
}
}
}

输出:

Java 中的 Else-If 语句

Java 中的 Else-If 语句

在编码示例1中,我们首先输入36作为数字,然后输入0作为数字。我们分别得到了完美的输出。当我们输入 36 作为数字时,我们得到的输出是该数字为正数。再次,我们输入一个数字为零,然后我们得到该数字为零的输出。

示例#2

在此编码示例中,我们检查 Else If 语句的功能,并查看一个人是否有资格献血。我们不使用 Buffered Reader 作为两个变量的输入。  我们直接输入到程序中,就得到了想要的结果。

说明 Else If 语句工作原理的 Java 程序

代码:

public class Age
{
public static void main(String[] args)
{
//Here the variable a is age and w is weight
int a=25;//Age
int w=48;// Weight
//Generating condition on age and weight
if(a>=18){
if(w>50)
{
System.out.println("You are eligible to donate blood");
} else
{
System.out.println("You are not eligible to donate blood");
}
} else
{
System.out.println("Age must be greater than 18");
}
}
}

输出:

Java 中的 Else-If 语句

在示例代码中,我们输入年龄为25,体重为48,并相应地执行程序。年龄大于18岁,符合献血条件。但体重低于节目要求的50,所以节目拒绝了该人献血。

示例 #3

在此程序中,我们根据用户输入的分数检查学生的成绩。等级为不及格、D、C、B、A 和 A+。

Java 程序,用于检查用户输入的特定考试中学生的成绩。

代码:

import java.io.*;
public class Exam
{
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter marks of the student in the exam");
int m=Integer.parseInt(br.readLine());
if(m<50)
{
System.out.println("The student has failed");
}
else if(m>=50 && m<60)
{
System.out.println("The student has got D grade");
}
else if(m>=60 && m<70)
{
System.out.println("The student has got C grade");
}
else if(m>=70 && m<80)
{
System.out.println("The student has got B grade");
}
else if(m>=80 && m<90)
{
System.out.println("The student has got A grade");
}
else if(m>=90 && m<100)
{
System.out.println("The student has got A+ grade");
}
else{
System.out.println("Invalid!");
}
}
}

输出:

Java 中的 Else-If 语句

Java 中的 Else-If 语句

在程序中,我们输入 65 和 80 作为数字。程序连续返回该学生在考试中分别获得了 C 级和 A 级。

结论

在本文中,我们检查了 Java 中 Else If 语句的功能,我们发现它只不过是一个在所有程序中都使用的多个 If 语句。我们还看到三个编码示例,它们非常详细地说明了 Else if 语句的功能。所有程序都广泛使用 Else If 语句,并以用户所需的方式打印输出。此外,只要有多个条件需要检查,就使用 Else if 语句。它们用于所有编程语言。

以上是Java 中的 Else-If 语句的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn