【问题标题】:I want to check if a number is binary or not in decimal numbers but it didnt work我想检查一个数字是否是二进制数或不是十进制数,但它不起作用
【发布时间】:2021-11-08 09:43:38
【问题描述】:

我想检查一个数字是否为二进制数或十进制数,但它不起作用 从编号 1 到 n。 例如从 1 到 10 我们有 2 个十进制数字,其中包含 0,1。我该如何更改它?

import java.util.Scanner;
public class Main
{
    public static void main(String args[]) {
        int r = 0, c = 0, num, b;
        int count=0;
        Scanner sl = new Scanner(System.in);
        System.out.println("Enter a number");
        num = sl.nextInt();

        for (int i = 1; i <= num; i++) {

                if ((i % 10 == 0) || (i % 10 == 1))
                    c++;
                r++;
                i = i / 10;
            }
            if (c == r)
                count++;
            else{

            }
        System.out.println(count);
        }

    }

【问题讨论】:

  • for (int i = 1; i &lt;= num; i++)i = i / 10; 的一些问题
  • 我应该如何处理它们? @sittsering
  • 只是一个问题,您是否找到了 1 到 n 范围内的二进制数?因为你必须使用 2 个循环。一个用于范围,一个检查其是否为二进制。
  • 是的@sittsering
  • 我改变了一些东西,你可以检查一下

标签: java


【解决方案1】:

我不是 java 开发者,所以也许我的回答不好。但是你可以使用你的数字作为 str 然后检查 str 是否仅由 0 和 1 构成

也许这会有所帮助: : How to check if only chosen characters are in a string?

祝你有美好的一天

【讨论】:

    【解决方案2】:

    以下是如何以优雅的方式做到这一点...

     // Function to check if number
        // is binary or not
        public static boolean isBinaryNumber(int num)
        {
    
            if(num == 1 || num == 0) return true
            if (num < 0) return false;
            
            // Get the rightmost digit of
            // the number with the help
            // of remainder '%' operator
            // by dividing it with 10
            while (num != 0) {
    
                // If the digit is greater
                // than 1 return false
                if (num % 10 > 1) {
                    return false;
                }
                num = num / 10;
            }
            return true;
        }
    

    【讨论】:

      【解决方案3】:

      在这里,将使用 2 个循环,一个用于范围,一个用于检查它是否为二进制。
      代替 c 和 r,我们将使用 flag 和 break 来跳过不必要的迭代。

      int count=0;
      boolean flag = true;
      for(int i=1;i<=num;i++)
      {
          for(int j=i;j>0;j=j/10)
          {
              // if remainder is not 0 and 1, then it means it's not binary
              // so we set flag as false
              // and using break to break out of the current(inner) loop, it's no longer needed to check remaining digits.
              if (j%10 > 1)
              {
                  flag = false;
                  break;
              }
          }
          // if flag is true, that means it's binary and we increment count.
          // if flag is flase, that means it's not binary 
          if(flag)
              count++;
          // here we reset flag back to true
          flag = true
      }
      System.out.println(count);
      

      您也可以按照@jchenaud 的建议进行操作。将其转换为字符串并检查它是否仅包含01

      【讨论】:

      • 哦,谢谢!这就是我要的。 @sittsering
      • @mary 可以正常工作吗?
      • 是的@sittsering
      • @mary 好的。然后接受它作为答案,这将关闭帖子
      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2023-03-19
      • 2013-09-12
      相关资源
      最近更新 更多