首页  >  文章  >  Java  >  Java 中的条件运算符

Java 中的条件运算符

WBOY
WBOY原创
2024-08-30 15:20:22505浏览

Java中的条件运算符是使用三个操作数的运算符,它们用于根据条件进行操作。它是一个独特的运算符,用于代替 If-Else 语句。条件运算符的主要优点是它只需一句话即可完成,而 if-else 语句则使用多行代码。但是,条件运算符也有一个缺点:它不能用于多个条件,或者换句话说,当使用多行代码时,程序会变得相当复杂并且很难理解。条件运算符在 Java 中也称为三元运算符。

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

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

条件运算符的语法

Java 中有一种使用三元或条件运算符的特定语法。下面给出了使用三元运算符的语法。通常,条件运算符用在 main() 中,或者用于在执行条件后返回某些值的特定函数。一旦执行了条件,则根据用户的命令执行并返回或打印满足条件的表达式。

变量=表达式1?表达式2 : 表达式3

上述条件运算符的工作方式与 If-Else 语句相同。相应的 if-else 语句如下:

语法:

if(Expression 1)
{
Variable= Expression 2;
}
else
{
Variable= Expression 3;
}

流程图:

Java 中的条件运算符

Java 中条件运算符的示例

以下是条件运算符的示例:

示例#1

在示例 1 中,我们将使用三元运算符查看两个数字之间的较大者。我们将看到两个数字变量的输入,然后检查在该上下文中哪个数字更大。我们可以看下面的例子,下面已经写好了

代码:

// Java program to find largest among two numbers using ternary operator
import java.io.*;
class Ternary
{
public static void main(String[] args)throws IOException
{
// Taking user input of the variables
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter first number " );
int n1= Integer.parseInt(br.readLine());
System.out.println("Enter second number " );
int n2= Integer.parseInt(br.readLine());
int max;//Variable to store the larger of the two values
// Larger among n1 and n2
max = (n1 > n2) ? n1 : n2; //Using ternary operator
// Print the largest number
System.out.println("Larger of the two numbers is " + max);
}
}

在上面的代码中,我们看到如何从用户那里取出两个数字,然后执行操作来计算两个数字中较大的一个。我们将看到该操作产生的两个输出。首先,我们将输入数字 (100,200),然后输入数字 (500, 200)。我们将看到相应的输出。

输出:

Java 中的条件运算符

Java 中的条件运算符

示例#2

在示例 2 中,我们将看到对两个变量执行的操作,这两个变量都是数字。如果第一个数字大于第二个数字,则将这两个数字相加并按照用户给出的方式打印。但是,如果第一个数字小于第二个数字,则从第一个数字减去第二个数字,然后打印结果。

代码:

// Java code to illustrate ternary operator
import java.io.*;
class Ternary2
{
public static void main(String[] args)throws IOException
{
// variable declaration
int res;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter First number: ");
int n1= Integer.parseInt(br.readLine());
System.out.println(" Enter Second number: ");
int n2= Integer.parseInt(br.readLine());
// Performing ternary operation
res = (n1 > n2) ? (n1 + n2) : (n1 - n2);//Calculating the sum
// Print the result
System.out.println("Result = " + res);
}
}

在这种情况下我们将看到一个输出。我们将输入两个数字(100,50)。由于第一个数字大于第二个数字,程序应该打印两个变量的总和,这就是输出。我们看到示例输出,如下所示。

输出:

Java 中的条件运算符

示例#3

在第三个示例中,我们将看到用户输入的三个数字,我们将检查这三个数字中最大的一个。同样,我们可以使用类似的逻辑找到三个数字中最小的一个。使用三元运算符的好处是可以节省程序内大量的代码,并且使得代码运行起来非常快速流畅。

代码:

//Program to Find greatest of three numbers using Conditional Operator
import java.io.*;
public class ConditionalOperator
{
public static void main(String[] args)throws IOException
{
int a,b,c,result;
//Taking input from the user
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Three Number : "); //prompt for input
a=Integer.parseInt(br.readLine());   //Read First number
b=Integer.parseInt(br.readLine());    //Read Second number
c=Integer.parseInt(br.readLine());    //Read third number
//Calculate the result based on conditional operator
result = (a>b)? ((a>c)?a:c) : ((b>c)?b:c);
System.out.println( result  + " is Greatest");//Printing the greatest number
}
}

现在,我们将输入三个数字(100,200,300),我们将看到三个数字中最大的一个,程序将打印该数字。按照程序,打印三个数字中最大的一个,最大的是300,打印顺利。这样程序就完美运行了。

输出:

Java 中的条件运算符

示例#4

在示例 4 中,我们将检查用户输入的三个数字中最小的一个。示例代码如下。

代码:

//Program to Find greatest of three numbers using Conditional Operator
import java.io.*;
public class ConditionalOperator
{
public static void main(String[] args)throws IOException
{
int a,b,c,result;
//Taking input from the user
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Three Number : "); //prompt for input
a=Integer.parseInt(br.readLine());   //Read First number
b=Integer.parseInt(br.readLine());    //Read Second number
c=Integer.parseInt(br.readLine());    //Read third number
//Calculate the result based on conditional operator
result = (a<b)? ((a<c)?a:c) : ((b<c)?b:c);
System.out.println( result  + " is Lowest");//Printing the greatest number
}
}

我们将输入三个数字并检查其中最小的一个。这三个数字是 (25,50,75),最小的数字应该是 25,如下面的输出所示。

输出:

Java 中的条件运算符

结论

在本文中,我们遇到了不同的程序,重点介绍了条件运算符的不同方面,并且我们看到了不同的功能以及使用条件运算符的不同优势。在 Java 编程语言中,条件运算符是独一无二的。它们取代了 If-Else 条件,并根据程序的条件顺利执行语句。

以上是Java 中的条件运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

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