[英]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.