检查整数以查看它是否包含零

发布于 2024-09-25 16:56:09 字数 140 浏览 5 评论 0原文

给定一个整数,如何使用 Java 检查它是否包含 0?

1 = Good
2 = Good
...
9 = Good
10 = BAD!
101 = BAD!
1026 = BAD!
1111 = Good

这怎么能做到呢?

Given an integer, how could you check if it contains a 0, using Java?

1 = Good
2 = Good
...
9 = Good
10 = BAD!
101 = BAD!
1026 = BAD!
1111 = Good

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦幻之岛 2024-10-02 16:56:09

你的意思是十进制表示中是否包含0?绝对最简单的方法是:

if (String.valueOf(x).contains("0"))

不要忘记数字并不“固有”包含或不包含0(当然,零本身除外) - 它取决于基数。因此,十进制的“10”是十六进制的“A”,十六进制的“10”是十进制的“16”......在这两种情况下,结果都会改变。

可能有更有效的方法来测试整数的十进制表示中是否存在零,但它们可能比上面的表达式更复杂。

Do you mean if the decimal representation contains a 0? The absolute simplest way of doing that is:

if (String.valueOf(x).contains("0"))

Don't forget that a number doesn't "inherently" contain a 0 or not (except for zero itself, of course) - it depends on the base. So "10" in decimal is "A" in hex, and "10" in hex is "16" in decimal... in both cases the result would change.

There may be more efficient ways of testing for the presence of a zero in the decimal representation of an integer, but they're likely to be considerably more involved that the expression above.

身边 2024-10-02 16:56:09

如果由于某种原因您不喜欢转换为字符串的解决方案,您可以尝试:

boolean containsZero(int num) {
    if(num == 0)
        return true;

    if(num < 0)
        num = -num;

    while(num > 0) {
        if(num % 10 == 0)
            return true;
        num /= 10;
    }
    return false;
}

这也假设 num 是以 10 为基数。

编辑:添加了处理负数和 0 本身的条件。

If for some reason you don't like the solution that converts to a String you can try:

boolean containsZero(int num) {
    if(num == 0)
        return true;

    if(num < 0)
        num = -num;

    while(num > 0) {
        if(num % 10 == 0)
            return true;
        num /= 10;
    }
    return false;
}

This is also assuming num is base 10.

Edit: added conditions to deal with negative numbers and 0 itself.

宫墨修音 2024-10-02 16:56:09

您可以将其转换为字符串并检查它是否包含字符“0”。

int number = 101;
if( ( "" + number ).contains( "0" ) ) {
  System.out.println( "contains the digit 0" );
}

You can convert it to a string and check if it contains the char "0".

int number = 101;
if( ( "" + number ).contains( "0" ) ) {
  System.out.println( "contains the digit 0" );
}
木緿 2024-10-02 16:56:09

Integer.toString(yourIntValue).contains("0");

Integer.toString(yourIntValue).contains("0");

漆黑的白昼 2024-10-02 16:56:09

这是一个用于检测整数中的零的例程。要使其适用于任何表示形式(十进制、十六进制、八进制、二进制),您需要传入基数作为参数。

public static boolean hasZero(int num, int base) {
    assert base > 0 : "must have positive non-zero base";

    if (num == 0)
        return true;

    while(num != 0) {
        if (num % base == 0) {
            return true;
        }
        else {
            num = num / base;
        }
    }

    return false;
}

public static void main(String args[]) {
    System.out.println(hasZero(10, 10));  // true (base 10 int)
    System.out.println(hasZero(-12, 10));  // false (base 10 int)

    System.out.println(hasZero(0x10, 16)); // true (hex is base 16)
    System.out.println(hasZero(0x1A, 16)); // false (hex is base 16)
}

Here is a routine that will work detect zeros in integers. To make it work with any representation (decimal, hex, octal, binary), you need to pass in the base as a parameter.

public static boolean hasZero(int num, int base) {
    assert base > 0 : "must have positive non-zero base";

    if (num == 0)
        return true;

    while(num != 0) {
        if (num % base == 0) {
            return true;
        }
        else {
            num = num / base;
        }
    }

    return false;
}

public static void main(String args[]) {
    System.out.println(hasZero(10, 10));  // true (base 10 int)
    System.out.println(hasZero(-12, 10));  // false (base 10 int)

    System.out.println(hasZero(0x10, 16)); // true (hex is base 16)
    System.out.println(hasZero(0x1A, 16)); // false (hex is base 16)
}
江挽川 2024-10-02 16:56:09

我不知道这是否更容易,但这是另一种方法。
将数字拆分为整数数组。然后排序并检查第一个元素是否为零。
例如

int n = 14501;
// after splitting
int na = {1, 4, 5, 0, 1};
// after sorting
int na = {0, 1, 1, 4, 5};

I don't know if this is easier but here is another way.
Split the number into an array of ints. Then sort and check if the first element is zero.
E.g

int n = 14501;
// after splitting
int na = {1, 4, 5, 0, 1};
// after sorting
int na = {0, 1, 1, 4, 5};
天涯沦落人 2024-10-02 16:56:09

不使用 Java,但从 C++ 转换并不难
附言。任何使用字符串转换的人都感到羞耻。

bool Contains0InBase10( unsigned int i, unsigned int& next )
{
 unsigned int divisor = 10;
 unsigned int remainder = 0;
 while( divisor <= i )
 {
  unsigned int newRemainder = i%divisor;
  if( newRemainder - remainder == 0)
  {
   // give back information allowing a program to skip closer to the next
   // number that doesn't contain 0
   next = i + (divisor / 10) - remainder;
   return true;
  }
  divisor *= 10;
  remainder = newRemainder;
 }
 return false;
}

Not using Java, but it's not exactly hard to convert from C++
PS. Shame on anyone using string conversion.

bool Contains0InBase10( unsigned int i, unsigned int& next )
{
 unsigned int divisor = 10;
 unsigned int remainder = 0;
 while( divisor <= i )
 {
  unsigned int newRemainder = i%divisor;
  if( newRemainder - remainder == 0)
  {
   // give back information allowing a program to skip closer to the next
   // number that doesn't contain 0
   next = i + (divisor / 10) - remainder;
   return true;
  }
  divisor *= 10;
  remainder = newRemainder;
 }
 return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文