Java - 检查输入是否为正整数、负整数、自然数等。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19766051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:02:49  来源:igfitidea点击:

Java - Check if input is a positive integer, negative integer, natural number and so on.

java

提问by kar

Is there any in-built method in Java where you can find the user input's type whether it is positive, or negative and so on? The code below doesn't work. I am trying to find a way to input any in-built method that can do it at the if statement.

Java 中是否有任何内置方法可以让您找到用户输入的类型,无论是正数还是负数等等?下面的代码不起作用。我试图找到一种方法来输入任何可以在 if 语句中执行的内置方法。

import java.util.Scanner;

public class Compare {

    public static void main(String[] args) { 

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = input.nextInt();

        if(number == int) 
            System.out.println("Number is natural and positive.");
    }
}

采纳答案by Juned Ahsan

If you really have to avoid operators then use Math.signum()

如果您真的必须避免使用运算符,请使用Math.signum()

Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.

返回参数的符号函数;如果参数为零,则为零,如果参数大于零,则为 1.0,如果参数小于零,则为 -1.0。

EDIT : As per the comments, this works for only double and float values. For integer values you can use the method:

编辑:根据评论,这仅适用于 double 和 float 值。对于整数值,您可以使用以下方法:

Integer.signum(int i)

Integer.signum(int i)

回答by blackpanther

What about using the following:

如何使用以下内容:

int number = input.nextInt();
if (number < 0) {
    // negative
} else {
   // it's a positive
}

回答by Theolodis

You could use if(number >= 0). The fact that you use int number = input.nextInt();makes sure that it has to be an Integer.

你可以使用if(number >= 0). 您使用的事实int number = input.nextInt();确保它必须是一个整数。

回答by Prabhakaran Ramaswamy

Use like below code.

使用如下代码。

if(number >=0 ) {
            System.out.println("Number is natural and positive.");
}

回答by Juned Ahsan

(You should you as Else-Ifstatement to check the for the three different state (positive, negative, 0)

(你应该作为Else-If语句来检查三种不同的状态(正、负、0)

Here is a simple example (excludes the possibility of non-integer values)

这是一个简单的例子(排除非整数值的可能性)

  import java.util.Scanner;

  public class Compare {

   public static void main(String[] args) { 

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a number: ");
    int number = input.nextInt();

    if( number == 0)
    { System.out.println("Number is equal to zero"); }
    else if (number > 0)
    { System.out.println("Number is positive"); }
    else 
    { System.out.println("Number is negative"); }


  }
 }

回答by Kefirchiks

For integers you can use Integer.signum()

对于整数,您可以使用Integer.signum()

Returns the signum function of the specified int value. (The return value is -1 if the specified value is negative; 0 if the specified value is zero; and 1 if the specified value is positive.)

返回指定 int 值的符号函数。(如果指定值为负,则返回值为 -1;如果指定值为 0,则返回值为 0;如果指定值为正,则返回值为 1。)