運算子被視為特殊字元或符號,用於對變數或值(運算元)執行某些操作。在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 |
此運算子檢查運算子左側的值是否等於右側的值。
範例:
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,因為它們不相等
情況 2:x = 4; y=4; 回傳 true,因為它們相等
此運算子檢查運算子左側的值是否不等於右側的值。
範例:
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,因為它們不相等
情況 2:x = 3; y=3; 回傳 false,因為它們相等
此運算子檢查運算子左側的值是否小於右側的值。
範例:
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
情況 2:x = 44; y=32; 當 x 不小於 y
時回傳 false此運算子檢查運算子左側的值是否大於右側的值。
範例:
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
情況 2:x = 43; y=57; 當 x 小於 y
時回傳 false此運算子檢查運算子左側的值是否小於或等於右側的值。
範例:
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情況 2:x = 45; y=54; 當 x 小於 y 時回傳 true
情況 3:x = 45; y=43; 當 x 大於 y
時回傳 false此運算子檢查運算子左側的值是否大於或等於右側的值。
範例:
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情況 2:x = 45; y=36; 當 x 大於 y 時回傳 true
情況 3:x = 55; y=55; 當 x 等於 y
時回傳 true以上是Java 中的比較運算符的詳細內容。更多資訊請關注PHP中文網其他相關文章!