logo

Java 정수 비교() 메소드

그만큼 비교하다() 방법은 다음의 방법이다. 정수 클래스 아래에 java.lang 패키지 . 이 방법은 두 정수 값을 수치적으로 비교합니다. 두 int 메소드를 비교하여 결과를 정수 상당 값으로 반환합니다. 인수 . 반환된 값은 다음에서 반환되는 값과 동일합니다.

Integer.valueOf(x).compareTo(Integer.valueOf(y))

통사론

다음은 선언문이다. 비교하다() 방법:

 public static int compare(int x,int y) 

매개변수:

데이터 형식 매개변수 설명 필수/선택
정수 엑스 비교할 첫 번째 정수 값 필수의
정수 그리고 비교할 두 번째 정수 값 필수의

보고:

이 메서드는 다음 값을 반환합니다.

 0 = The value 0 if x == y, -1 = The value less than 0 if x y 

예외:

저것

호환성 버전:

자바 1.7 이상

실시예 1

 public class IntegerCompareExample1 { public static void main(String[] args) { int num1 = 10; int num2 = 20; int num3 = 10; int num4 = 30; // as num1 less than num2, Output will be less than zero System.out.println(Integer.compare(num1, num2)); // as num1 equals num3, Output will be zero System.out.println(Integer.compare(num1, num3)); // as num4 is greater than num2, Output will be greater than zero System.out.println(Integer.compare(num4, num2)); } } 
지금 테스트해보세요

산출:

 -1 0 1 

실시예 2

 import java.util.Scanner; public class IntegerCompareExample2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the first numeric value: &apos;); int number1 = sc.nextInt(); System.out.print(&apos;Enter the second numeric value: &apos;); int number2 = sc.nextInt(); sc.close(); // compares two Integer values numerically int retResult = Integer.compare(number1, number2); if(retResult &gt; 0) { System.out.println(&apos;number1 is greater than number2&apos;); } else if(retResult<0) { system.out.println('number1 is less than number2'); } else equal to < pre> <p> <strong>Output:</strong> </p> <pre> Enter the first numeric value: 55 Enter the second numeric value: 66 number1 is less than number2 </pre> <h2>Example 3</h2> <pre> public class IntegerCompareExample3 { public static void main(String[] args) { System.out.println(Integer.compare(200, 100)); System.out.println(Integer.compare(200, 200)); System.out.println(Integer.compare(100, 150)); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 0 -1 </pre> <br></0)>

실시예 3

 public class IntegerCompareExample3 { public static void main(String[] args) { System.out.println(Integer.compare(200, 100)); System.out.println(Integer.compare(200, 200)); System.out.println(Integer.compare(100, 150)); } } 
지금 테스트해보세요

산출:

 1 0 -1