📜  Java中的整数signum()方法

📅  最后修改于: 2022-05-13 01:54:36.941000             🧑  作者: Mango

Java中的整数signum()方法

符号函数是一个奇数数学函数,它提取实数的符号。符号函数也称为符号函数。
Java的Integer.signum()方法返回指定整数值的signum函数。对于正值、负值和零,该方法分别返回 1、-1 和 0。

句法 :

public static int signum(int a)

参数:该方法采用一个整数类型的参数a ,将对其执行操作。

返回值:该方法返回指定整数值的符号函数。如果指定值为负,则返回 -1,如果指定值为 0,则返回 0,如果指定值为正,则返回 1。

例子:

Consider an integer a = 27
Since 27 is a positive int signum will return= 1
        
Consider another integer a = -61
Since -61 is a negative int signum will return= -1
     
Consider the integer value a = 0
For a zero signum, it will return = 0 

下面的程序说明了Java.lang.Integer.signum() 方法:

方案一:

Java
// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    // It will return 1 as  the int value is greater than 1
    System.out.println(Integer.signum(1726));
 
    // It will return -1 as  the int value is less than 1
    System.out.println(Integer.signum(-13));
 
    // It will returns 0 as int value is equal to 0
    System.out.println(Integer.signum(0));
}
}


Java
// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    // It will return 1 as  the int value is greater than 1
    System.out.println(Integer.signum(-1726));
 
    // It will return -1 as  the int value is less than 1
    System.out.println(Integer.signum(13));
 
    // It will returns 0 as int value is equal to 0
    System.out.println(Integer.signum(0));
}
}


Java
// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    //returns compile time error as passed argument is a decimal value
    System.out.println(Integer.signum(3.81));
     
     
    //returns compile time error as passed argument is a string
    System.out.println(Integer.signum("77"));
 
 
}
}


输出:
1
-1
0

方案二:

Java

// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    // It will return 1 as  the int value is greater than 1
    System.out.println(Integer.signum(-1726));
 
    // It will return -1 as  the int value is less than 1
    System.out.println(Integer.signum(13));
 
    // It will returns 0 as int value is equal to 0
    System.out.println(Integer.signum(0));
}
}
输出:
-1
1
0

程序 3:对于十进制值和字符串。
注意:当十进制值和字符串作为参数传递时,它会返回错误消息。

Java

// Java program to illustrate the
// Java.lang.Integer.signum() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    //returns compile time error as passed argument is a decimal value
    System.out.println(Integer.signum(3.81));
     
     
    //returns compile time error as passed argument is a string
    System.out.println(Integer.signum("77"));
 
 
}
}
输出:
prog.java:10: error: incompatible types: possible lossy conversion from double to int
    System.out.println(Integer.signum(3.81));
                                      ^
prog.java:14: error: incompatible types: String cannot be converted to int
    System.out.println(Integer.signum("77"));
                                      ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors