Java中的整数signum()方法

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

null

语法:

public static int signum(int a)

参数: 该方法采用一个参数 A. 要对其执行操作的整数类型。

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

例如:

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

下面的程序演示了Java。整型。signum()方法:

项目1:

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-10

项目2:

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 ));
}
}


输出:

-110

方案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 output2 errors

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享