ホームページ  >  記事  >  Java  >  Java の比較演算子

Java の比較演算子

WBOY
WBOYオリジナル
2024-08-30 15:19:30604ブラウズ

演算子は、変数または値 (オペランド) に対して特定の演算を実行するために使用される特殊文字または記号とみなされます。 Java では、変数を操作するために使用される演算子がいくつかあります。これには、算術演算子、ビット演算子、比較演算子、論理演算子、その他が含まれます。この記事では、Java の比較演算子について詳しく説明します。

Java の比較演算子

次に、Java のさまざまな比較演算子を示します。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト
Name of the Operator Operator Example
Equal to = = a= =b
Not equal to != a!=b
Less than < a
Greater than > a>b
Less than or equal to <= a<=b
Greater than or equal to >= a>=b

1.

と等しい

この演算子は、演算子の左側の値が右側の値と等しいかどうかをチェックします。

例:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user and store it in variable x
System.out.print("Enter the value of x : ");
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//checks whether x and y are equal; Return true if it is same, else returns false
System.out.println(x == y);
}
}

出力:

ケース 1: x = 3;  y =5;  等しくないため false を返します

Java の比較演算子

ケース 2: x = 4;  y = 4;  等しい場合は true を返します

Java の比較演算子

2.

に等しくない

この演算子は、演算子の左側の値が右側の値と等しくないかどうかをチェックします。

例:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user and store it in variable x
System.out.print("Enter the value of x : ");
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//checks whether x and y are not equal; Return true if it is not equal, else returns false
System.out.println(x != y);
}
}

出力:

ケース 1: x = 3;  y = 4;  等しくない場合は true を返します

Java の比較演算子

ケース 2: x = 3;  y = 3;  等しいため false を返します

Java の比較演算子

3.

未満

この演算子は、演算子の左側の値が右側の値より小さいかどうかをチェックします。

例:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user
System.out.print("Enter the value of x : ");
//store the value in variable x
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//Returns true if x is less than y, else false
System.out.println(x < y);
}
}

出力:

ケース 1: x = 4;  y = 6;  x が y

より小さい場合、true を返します。

Java の比較演算子

ケース 2: x = 44;  y = 32;  x が y

以上であるため false を返します

Java の比較演算子

4.

より大きい

この演算子は、演算子の左側の値が右側の値より大きいかどうかをチェックします。

例:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.<em>in</em>);
//take the value of x as input from user
System.out.print("Enter the value of x : ");
//store the value in variable x
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//Returns true if x is greater than y, else false
System.out.println(x > y);
}
}

出力:

ケース 1: x = 67;  y = 66;  x が y

より大きい場合に true を返します

Java の比較演算子

ケース 2: x = 43;  y = 57;  x が y

未満であるため false を返します

Java の比較演算子

5.以下

この演算子は、演算子の左側の値が右側の値以下であるかどうかをチェックします。

例:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user and store it in variable x
System.out.print("Enter the value of x : ");
x = sc.nextInt();
//take the value of y as input from user and store it in variable y
System.out.print("Enter the value of y : ");
y = sc.nextInt();
//Returns true x is less than or equal to y, else false
System.out.println(x <= y);
}
}

出力:

ケース 1: x = 45;  y = 45;  x が y

に等しいため true を返します

Java の比較演算子

ケース 2: x = 45;  y = 54;  x が y

より小さい場合、true を返します。

Java の比較演算子

ケース 3: x = 45;  y = 43;  x が y

より大きい場合は false を返します

Java の比較演算子

6.以上

この演算子は、演算子の左側の値が右側の値以上であるかどうかをチェックします。

例:

import java.util.Scanner;
public class ComparisonExample {
public static void main(String[] args) {
int x, y;
Scanner sc= new Scanner(System.in);
//take the value of x as input from user
System.out.print("Enter the value of x : ");
//store the value in variable x
x = sc.nextInt();
//take the value of y as input from user
System.out.print("Enter the value of y : ");
//store the value in variable y
y = sc.nextInt();
//Returns true x is greater than or equal to y, else false
System.out.println(x >= y);
}
}

出力:

ケース 1: x = 54;  y = 67;  x が y

未満であるため false を返します

Java の比較演算子

ケース 2: x = 45;  y = 36;  x が y

より大きい場合に true を返します

Java の比較演算子

ケース 3: x = 55;  y =55;  x が y

に等しい場合、true を返します。

Java の比較演算子

以上がJava の比較演算子の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。