1. Overview
1.概述
In Java, when we work with types like Integer, Long, Float, and Double, we often want to check if the numbers are positive or negative. This is a fundamental and common number operation.
在Java中,当我们使用Integer、Long、Float和Double等类型时,我们经常要检查数字是正还是负。这是一个基本和常见的数字操作。
In this quick tutorial, we’ll discuss how to check whether a given number is positive or negative.
在这个快速教程中,我们将讨论如何检查一个给定的数字是正还是负。
2. Introduction to the Problem
2.对问题的介绍
Checking whether a number is positive or negative is a pretty straightforward problem. However, before we start looking at the implementations, let’s understand the definition of positive and negative.
检查一个数字是正数还是负数是一个相当直接的问题。然而,在我们开始研究实现方法之前,让我们了解一下正数和负数的定义。
Given a real number n, if n is greater than zero, it’s a positive number. Otherwise, if n is less than zero, it’s negative. So, we still have a particular case: zero. Zero is neither positive nor negative.
给定一个实数 n,如果n大于零,它就是一个正数。否则,如果n小于零,它就是负数。所以,我们仍然有一个特殊情况:零。零既不是正数也不是负数。
So, we can create an enum to cover these three possibilities:
因此,我们可以创建一个enum来涵盖这三种可能性。
enum Result {
POSITIVE, NEGATIVE, ZERO
}
In this tutorial, we’ll address two different ways to check if a number is positive, negative, or zero. For simplicity, we’ll use unit test assertions to verify the result.
在本教程中,我们将讨论两种不同的方法来检查一个数字是否为正数、负数或零。为了简单起见,我们将使用单元测试断言来验证结果。
So next, let’s see them in action.
那么接下来,让我们看看他们的行动。
3. Using the ‘<‘ and the ‘>‘ Operators
3.使用”<“和”>“操作符
Per definition, whether a number is positive or negative depends on the result of the comparison to zero. Therefore, we can use Java’s “greater than (>)” and “less than (<)” operators to solve the problem.
根据定义,一个数字是正数还是负数,取决于与零比较的结果。因此,我们可以使用Java的 “大于(>)”和 “小于(<)”operators来解决这个问题。
Next, let’s take the Integer type as an example to create a method to do the check:
接下来,让我们以Integer类型为例,创建一个方法来做检查。
static Result byOperator(Integer integer) {
if (integer > 0) {
return POSITIVE;
} else if (integer < 0) {
return NEGATIVE;
}
return ZERO;
}
The code above explains itself clearly. Depending on the result of the comparison to zero, we determine if the result is positive, negative, or zero.
上面的代码可以清楚地解释自己。根据与零比较的结果,我们确定结果是正数、负数还是零。
Let’s create a test to verify our method:
让我们创建一个测试来验证我们的方法。
assertEquals(POSITIVE, PositiveOrNegative.byOperator(42));
assertEquals(ZERO, PositiveOrNegative.byOperator(0));
assertEquals(NEGATIVE, PositiveOrNegative.byOperator(-700));
Unsurprisingly, the test passes if we execute it.
不出所料,如果我们执行该测试,它就会通过。
Of course, the same logic works if we can change the Integer parameter to Long, Float, or Double.
当然,如果我们可以将Integer参数改为Long、Float或Double,同样的逻辑也是可行的。
4. Using the signum() Method
4.使用signum()方法
We’ve seen how to check if a number is positive or negative using the < and the > operators. Alternatively, we can use the signum() method to get the sign of the given number.
我们已经看到了如何使用<和>运算符来检查一个数字是正还是负。另外,我们可以使用signum()方法来获取给定数字的符号。
For Integer and Long numbers, we can call the Integer.signum() and Long.signum() methods.
对于Integer和Long数字,我们可以调用Integer.signum()和Long.signum()方法。
The signum(n) method returns -1, 0, and 1 when n is negative, zero, or positive.
当n为负数、零数或正数时,signum(n)方法返回-1、0和1。
Let’s take an Integer as an example to create a check method:
让我们以一个Integer为例来创建一个检查方法。
static Result bySignum(Integer integer) {
int result = Integer.signum(integer);
if (result == 1) {
return Result.POSITIVE;
} else if (result == -1) {
return Result.NEGATIVE;
}
return Result.ZERO;
}
The test below verifies our method works as expected:
下面的测试验证了我们的方法如预期般工作。
assertEquals(POSITIVE, PositiveOrNegative.bySignum(42));
assertEquals(ZERO, PositiveOrNegative.bySignum(0));
assertEquals(NEGATIVE, PositiveOrNegative.bySignum(-700));
Unlike Integer and Long, Float and Double classes don’t provide the signum() method. However, the Math.signum() method accepts Float and Double numbers as the parameter, for example:
与Integer和Long不同,Float和Double类没有提供signum()方法。然而,例如,Math.signum()方法接受Float和Double数字作为参数。
static Result bySignum(Float floatNumber) {
float result = Math.signum(floatNumber);
if (result.compareTo(1.0f) == 0) {
return Result.POSITIVE;
} else if (result.compareTo(-1.0f) == 0) {
return Result.NEGATIVE;
}
return Result.ZERO;
}
Finally, let’s create a test to verify if the method can check if a float number is positive or negative:
最后,让我们创建一个测试来验证该方法是否能够检查一个浮点数是正还是负。
assertEquals(POSITIVE, PositiveOrNegative.bySignum(4.2f));
assertEquals(ZERO, PositiveOrNegative.bySignum(0f));
assertEquals(NEGATIVE, PositiveOrNegative.bySignum(-7.7f));
The test passes if we give it a run.
如果我们让它运行一下,测试就会通过。
5. Conclusion
5.总结
In this article, we’ve learned two ways to determine whether a given number is positive, negative, or zero.
在这篇文章中,我们已经学会了两种方法来确定一个给定的数字是正数、负数还是零。
As usual, all code snippets presented here are available over on GitHub.
像往常一样,这里介绍的所有代码片段都可以在GitHub上找到。。