Java 程序:使用关系运算符

原文:https://www.studytonight.com/java-programs/java-program-to-use-relational-operators

在本教程中,我们将学习如何执行关系操作。Java 中的关系运算符用于比较等式、小于、大于等变量。它总是返回一个布尔变量。但是在继续之前,如果您不熟悉 java 中关系运算符的概念,那么一定要查看关于 Java 中运算符的文章。

输入:输入第一个数字(num1): 6

输入第二个数字(num2): 2

输出:

==之后的结果是:假

结果之后!=是:真的

num1>num2 后的结果:真

num1

num1>=num2 后的结果:真

num1<=num2 后的结果:假

上述问题可以通过以下方式解决:

方法 1:当值被预定义时

方法 2:当值由用户定义时

让我们分别看看这些方法。

程序 1:执行关系操作

在这个程序中,我们将在程序中预定义值时执行关系操作。

算法:

  1. 开始
  2. 这里,我们将使用 switch case 从不同的关系运算符中进行选择,如==,!=,,<= and > =。
  3. 声明两个变量。
  4. 初始化它。
  5. 执行所有关系运算符,如==,!=,,<= and > =。
  6. 显示每个关系操作的结果。
  7. 停下来。

让我们看看下面的例子,以便更好地理解。

//Java Program to perform relational operations
public class Main
{
   public static void main(String args[]) 
   {
      //Declare and initialize the variables
      int a = 6;
      System.out.println("The entered value of a is " + a);
      int b = 2;
      System.out.println("The entered value of b is " + b);
      //Perform relational operations
      System.out.println("Values of Relational Operations: ");
      System.out.println("The result of a == b is " + (a == b) );
      System.out.println("The result of a != b is " + (a != b) );
      System.out.println("The result of a > b is " + (a > b) );
      System.out.println("The result of a < b is " + (a < b) );
      System.out.println("The result of b >= a is " + (b >= a) );
      System.out.println("The result of b <= a is " + (b <= a) );
   }
}

a 的输入值为 6 b 的输入值为 2 关系运算的值: a = = b 的结果为假 a 的结果!= b 为真 a>b 的结果为真 aa 的结果为假 b<a 的结果为真

程序 2:执行关系操作

在这个程序中,我们将通过接收用户的输入来执行关系操作。

算法:

  1. 开始
  2. 这里,我们将使用 switch case 从不同的关系运算符中进行选择,如==,!=,,<= and > =。
  3. 为相同的声明一个变量。
  4. 请用户初始化它。
  5. 根据选择的操作,声明两个变量。
  6. 要求用户初始化变量。
  7. 执行关系操作后显示结果。
  8. 停下来。

让我们看看下面的例子,以便更好地理解。

//Java program to perform Relational Operators
import java.util.Scanner;
public class Main
{
   public static void main(String args[])
   {   
        Scanner s = new Scanner(System.in);
        while(true)
        {
            System.out.println("");

            System.out.println("Choose the operation you want to perform ");
            System.out.println("Choose 1 for  == ");
            System.out.println("Choose 2 for != ");
            System.out.println("Choose 3 for > ");
            System.out.println("Choose 4 for < ");
            System.out.println("Choose 5 for <= ");
            System.out.println("Choose 6 for >= ");
            System.out.println("Choose 7 for EXIT");
            int n = s.nextInt();
            switch(n)
            {
                case 1:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int a = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int b = s.nextInt();
                    System.out.println("The result after == is: "+(a == b));
                    break;

                case 2:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int p = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int q = s.nextInt();
                    System.out.println("The result after != is: "+(p != q));
                    break;

                case 3:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int x = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int y = s.nextInt();
                    System.out.println("The result after > is : "+(x > y));
                    break;

                case 4:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int c = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int d = s.nextInt();
                    System.out.print("The result after < is : "+(c < d));
                    break;

                case 5:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int e = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int f = s.nextInt();
                    System.out.println("The result after >= : "+(e >= f));
                    break;

                case 6:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int g = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int h = s.nextInt();
                    System.out.println("The result after <= : "+(g <= h));
                    break;

                case 7:
                    System.exit(0);
            }
        }
    }
}

选择要执行的操作 选择 1 =选择= 选择 2!= 选择 3 代表> 选择 4 代表< 选择 5 代表< = 选择 6 代表> = 选择 7 代表退出 1 输入两个数字执行操作 输入第一个数字:3 输入第二个数字:2 之后的结果==为:假

选择要执行的操作 选择 1 代表== 选择 2 代表= 选择 3 为> 选择 4 为< 选择 5 为< = 选择 6 为> = 选择 7 为退出 4 输入两个数字执行操作 输入第一个数字:2 输入第二个数字:7 在<后的结果为:真 选择要执行的操作 选择 1 为== 选择= 选择 3 表示> 选择 4 表示< 选择 5 表示< = 选择 6 表示> = 选择 7 表示退出 7