[英]Convert a binary array to decimal in JAVA [closed]
I am trying to make a method which receives an array as a parameter and through a recursive method, that same number can be returned but in decimal notation.我正在尝试创建一个接收数组作为参数的方法,并通过递归方法,可以返回相同的数字,但以十进制表示。
Basically first I am instantiating this array:基本上首先我实例化这个数组:
int[] binario = {1, 1, 0, 1, 0, 1};
And this is my method这是我的方法
public static int convertirBinarioADecimal(int[] bin, int i, int decimal) {
if (i < bin.length) {
if (bin[i] == 1) {
decimal += Math.pow(2, ((bin.length - 1) - i));
}
return convertirBinarioADecimal(bin, i++, decimal);
} else {
return decimal;
}
}
This is the call to the method on main:这是对 main 方法的调用:
res = convertirBinarioADecimal(binario, 0, decimal);
System.out.println("res = " + res);
However, this gives me an error every time that I run it and dont understand why.但是,每次我运行它并且不明白为什么时,这都会给我一个错误。 Could you please take a look into that code and tell what is wrong?您能否查看该代码并告诉您有什么问题?
My apologies for the basics of the question but I had quit my programming studies for a couple of years but now I came back and it is causing me some problems Thanks in advance for the help对于这个问题的基础知识,我深表歉意,但我已经放弃了几年的编程研究,但现在我回来了,这给我带来了一些问题提前感谢您的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.