Java 程序:使用条件运算符
原文:https://www.studytonight.com/java-programs/java-program-to-use-conditional-operator
在本教程中,我们将学习如何执行条件运算。条件运算符由三个操作数组成,用于计算布尔表达式。这个操作符的目标是决定;应该将哪个值赋给变量。它也被称为三元运算符。但是在继续之前,如果你不熟悉 java 中条件运算符的概念,那么一定要查看关于 Java 中运算符的文章。
输入:
输入第一个数字:4
输入第二个数字:4
字符串输出= (4==4)?“平等”:“不平等”
输出:相等
上述问题出现了两种情况:
情况 1:当值由用户定义时
情况 2:当值被预定义时
让我们分别看看这些案例。
程序 1:执行条件运算
在这个程序中,我们将看到当值是用户定义的时,如何执行条件“与”和条件“或”运算。这里,我们将首先要求用户输入值,然后执行条件“与”和条件“或”操作。
算法:
- 开始
- 创建 Scanner 类的实例。
- 声明三个变量。
- 要求用户初始化变量。
- 执行条件与运算和条件或运算。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program to perform Conditional AND and OR operations
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int x=sc.nextInt(); //Declare and Initialize the number
System.out.print("Enter the second number: ");
int y=sc.nextInt(); //Declare and Initialize the number
System.out.print("Enter the third number: ");
int z=sc.nextInt(); //Declare and Initialize the number
//Conditional AND Operator
//Conditional OR Operator
System.out.println("Result of : "+x+">"+y+" && "+x+">"+z+" || "+y+"<"+z);
System.out.println(x>y && x>z || y<z);
System.out.println("Result of ("+x+"<"+z+" || "+y+">"+z+") && "+x+"<"+y);
System.out.println((x<z || y>z) && x<y);
}
}
输入第一个数字:12 输入第二个数字:11 输入第三个数字:10 结果为:12>11&T9】12>10 | | 11<10 真 结果为(12<10 | | 11>10)&T15】12<11 假
程序 2:执行条件运算
在这个程序中,我们将看到当值在程序中预先定义时,如何执行条件“与”和条件“或”运算。
算法:
- 开始
- 声明三个变量。
- 初始化这些变量。
- 执行条件与运算和条件或运算。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program to perform Conditional AND and OR operations
import java.util.*;
public class Main
{
public static void main(String args[])
{
int x= 9,y=8,z=6;
//Conditional AND Operator
//Conditional OR Operator
System.out.println("Result of : "+x+">"+y+" && "+x+">"+z+" || "+y+"<"+z);
System.out.println(x>y && x>z || y<z);
System.out.println("Result of ("+x+"<"+z+" || "+y+">"+z+") && "+x+"<"+y);
System.out.println((x<z || y>z) && x<y);
}
}
结果为:9>8&T6】9>6 | | 8<6 真 结果为(9<6 | | 8>6)&T12】9<8 假
程序 3:执行条件运算
在这个程序中,我们将看到当值是用户定义的时,如何执行三元运算。这里,我们将首先要求用户输入值,然后我们将使用三元运算符检查指定的条件,如果条件为真,将显示第一个表达式,如果条件为假,将显示第二个表达式。
算法:
- 开始
- 创建 Scanner 类的实例。
- 声明两个变量。
- 要求用户初始化变量。
- 使用三元运算符检查条件。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program to perform ternary operation
import java.util.*;
public class Main
{
public static void main(String args[])
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.print("Enter the first number: ");
int a=sc.nextInt(); //Declare and Initialize the number
System.out.print("Enter the second number: ");
int b=sc.nextInt(); //Declare and Initialize the number
String out = a==b ? "Yes":"No";
System.out.println("Is "+a+" == "+b+ "?");
System.out.println(out);
}
}
输入第一个数字:12 输入第二个数字:11 是 12 == 11? 否
程序 4:执行条件运算
在这个程序中,我们将看到当值在程序中被预定义时,如何执行三元运算。
算法:
- 开始
- 声明两个变量。
- 初始化变量。
- 使用三元运算符检查条件。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program to perform ternary operator
public class Main
{
public static void main(String args[])
{
int a=9,b=8;
System.out.print("The entered number is: "+a);
System.out.print("The entered number is: "+b);
String out = a>=b ? "Yes both the numbers are the same":"No both the numbers are not the same";
System.out.println("Is "+a+" >= "+b+ "?");
System.out.println(out);
}
}
输入的数字是:9 输入的数字是:8 是 9 > = 8? 是的,两个数字相同