简体   繁体   English

我该如何解决这个问题我想将十进制数转换为二进制数

[英]how can i fix this i want to convert decimal number to binary

public class Binar{
public static void main(String[] args){
    int num = 7;
    long Binary = cBtD(num);
System.out.printf("%d numri decimal = %d binar" , num, Binary);
}
public static long cBtD(int num){
    long BinaryNumber = 0;
    int  i = 0;
    long reminder;
    while(num > 0){
        reminder = num % 2;
        num /= 2;
        ++i;
    }
  for (int j = i - 1; j >= 0; j--) {
        System.out.print(BinaryNumber[j]); 
  }
    return BinaryNumber;
}}

and i have this error and it says "array required, but long found" and "System.out.print(BinaryNumber[j]);"我有这个错误,它说“需要数组,但找到很久了”和“System.out.print(BinaryNumber[j]);”

Reason behind this error is, you have defined BinaryNumber variable as long and it is not an array.此错误背后的原因是,您已将BinaryNumber变量定义为long并且它不是数组。 But you are trying to access it like an array.但是您正试图像访问数组一样访问它。 Please check my modified answer below:请在下面查看我修改后的答案:

public class Binar {
public static void main(String[] args) {
    int num = 7;
    String Binary = cBtD(num);
    System.out.printf("%d numri decimal = %s binar", num, Binary);
}

public static String cBtD(int num) {
    String BinaryNumber = "";
    long reminder;
    if (num == 0) {
        return "0";
    }
    while (num > 0) {
        reminder = num % 2;
        BinaryNumber = String.valueOf(reminder).concat(BinaryNumber);
        num /= 2;
    }
    return BinaryNumber;
}
}

That error occurred because you defined BinaryNumber's type 'long' and you wanted use it as an array.发生该错误是因为您定义了 BinaryNumber 的类型 'long' 并且您想将其用作数组。

I change it a bit, try it:我改了一下,试试看:

public class Binar {

    public static void main(String[] args) {
        int num = 7;
        int[] binaryArray = cBtD(num);
        String numbers = "";
        for (int aBinaryArray : binaryArray)
            numbers += aBinaryArray;

        System.out.printf("%d numri decimal = %d binar" , num, Integer.parseInt(numbers));
    }

    private static int[] cBtD(int num){
        int i = 0;
        int temp[] = new int[7];
        int binaryNumber[];
        while (num > 0) {
            temp[i++] = num % 2;
            num /= 2;
        }
        binaryNumber = new int[i];
        int k = 0;
        for (int j = i - 1; j >= 0; j--) {
            binaryNumber[k++] = temp[j];
        }
        return binaryNumber;
    }
}

Or you can simply use these methods to convert decimal to binary:或者您可以简单地使用这些方法将十进制转换为二进制:

Integer.toBinaryString();

Or this:或这个:

Integer.toString(n,2);

All numbers are inherently binary.所有数字本质上都是二进制的。 But whether you display them in binary or hex or octal is simply a matter of representation.但是,无论您以二进制、十六进制还是八进制显示它们都只是一个表示问题。 Which means you want to print them as a string.这意味着您想将它们打印为字符串。 Even when you do the following:即使您执行以下操作:

int v = 123;
System.out.println(v); // v is printed as a decimal string.

So to convert them to a binary string, just prepend the remainders to the string after dividing by two (via the remainder operator).因此,要将它们转换为二进制字符串,只需在除以二(通过余数运算符)后将余数添加到字符串中。

int n = 11;
String s = "";

s = (n%2) + s; // s = "1"
n/=2;  // n == 5
s = (n%2) + s; // s = "11"
n/=2   // n == 2
s = (n%2) + s; // s = "011";
n/=2   // n == 1
s = (n%2) + s; // s = "1011";
n/=2;  // n == 0

n == 0 so your done.

return s and print it.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将十进制数转换为正好8位的二进制数? - How I can convert a decimal number to exactly 8 bit binary? 如何将整数从十进制转换为二进制 - How can I convert an int number from decimal to binary 如何将整数从十进制转换为二进制? - How can I convert an int number from decimal to binary? 如何解决将String中的Number转换为正确的十进制数字的方法? - How can I fix this method that convert Number in String to show the correct number of decimal digits? 我无法将二进制转换为十进制,也无法将十进制转换为二进制 - I can't convert Binary to Decimal, Decimal to Binary 如何在Java中将非常大的十进制数转换为二进制数 - How Can I Convert Very Large Decimal Numbers to Binary In Java 我想检查一个数字是否是二进制数或不是十进制数,但它没有用 - I want to check if a number is binary or not in decimal numbers but it didnt work 我想将二进制字符串转换为相应的十六进制字符串,但是当输入的二进制字符串很长时,则会显示NumberFormatException。如何解决? - I want to convert a binary string to corresponding hex string.But when the input binary string long then NumberFormatException is shown.How to fix it? 如何在Java中将小数转换为二进制? - How do I convert a decimal fraction to binary in Java? 我创建了十进制到二进制方法,我想在2D数组中使用它,但是我不能 - I created decimal to binary method and I want to use it in 2D array but I can't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM