首頁 > Java > java教程 > 主體

Java 中的比較運算符

WBOY
發布: 2024-08-30 15:19:30
原創
563 人瀏覽過

運算子被視為特殊字元或符號,用於對變數或值(運算元)執行某些操作。在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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!