Java 奇数成功转换为二进制,偶数失败
我的Java程序有问题。我制作了一个程序,它接受一个整数并将其转换为二进制值。当数字为奇数时,没有任何问题。15个转换为1111,17个转换为10001,依此类推。当数字为偶数时,问题就出现了。如果我输入16、18、20等等,它只返回0。每一次。另外,重要的是要注意,我通过使用递归方法获取数字,该方法在到达数字时停止 这是我的密码。感谢任何能给我的帮助,即使它不能解决问题Java 奇数成功转换为二进制,偶数失败,java,recursion,binary,Java,Recursion,Binary,我的Java程序有问题。我制作了一个程序,它接受一个整数并将其转换为二进制值。当数字为奇数时,没有任何问题。15个转换为1111,17个转换为10001,依此类推。当数字为偶数时,问题就出现了。如果我输入16、18、20等等,它只返回0。每一次。另外,重要的是要注意,我通过使用递归方法获取数字,该方法在到达数字时停止 这是我的密码。感谢任何能给我的帮助,即使它不能解决问题 public class binaryConverter { static int nr = 16; sta
public class binaryConverter {
static int nr = 16;
static int max = 0;
static int[] bin;
static int[] array;
static int finalBin;
public static void main(String[] args) {
maxFinder();
binMaker();
toBinary(array, 0,true);
}
//finds out how many binary numbers are used in order to decide what length to make the array,
//15 = 1111, 10000 = 16 15<16
private static void maxFinder(){
for(int i = 0, n = 1; i<nr; i++){
if(n>nr){
max = i;
break;
}
n*=2; //n doubles for every i loop, starts with one
}
}
//makes a library of standard to binary (0 = 1, 1 = 2; 2 = 4; 3 = 8...)
private static void binMaker(){
int[] temp = new int[max];
for(int i = 0; i<temp.length; i++){
if(i == 0) temp[i] = 1;
else temp[i]=2*temp[i-1];
}
bin = temp;
array = new int[bin.length];
}
//adds the array together in order to access what number the array currently resembles in binary
private static int sum(int[] ar, int length){
int sum = 0;
for(int i = 0; i<=length; i++) if(ar[i]==1) sum += bin[i];
return sum;
}
//loops until the array becomes the number in binary
private static void toBinary(int[] ar, int i, boolean one){ //i = the current number it's on, eg. 10i01, i is the third slot
if(i==array.length) return; //break if
ar[i] = (one) ? 1:0;
if(sum(ar, i)==nr){ //if the temporary array is the number but in binary ...
array = ar; //turns the static array into the temporary array
String temp = "";
for(int z = 0; z<array.length; z++) temp += array[z];
finalBin = Integer.parseInt(temp); //makes finalBin represent the original number but in binary
return;
}
else{ //else go to the next slot
toBinary(ar, i+1, true);
toBinary(ar, i+1, false);
}
}
}
公共类二进制转换器{
静态整数nr=16;
静态int max=0;
静态int[]bin;
静态int[]数组;
静态int-finalBin;
公共静态void main(字符串[]args){
maxFinder();
binMaker();
toBinary(数组,0,true);
}
//找出使用了多少二进制数来决定数组的长度,
//15=1111,10000=16 15开始递归时,总是在二进制文件的第一位加一:
toBinary(数组,0,true);
这样你就永远不会得到偶数。偶数的“第一”位总是有一个零(表示“2到0的幂”)
可以这样开始递归:
toBinary(数组,0,true);
如果(/*未找到解决方案*/)
toBinary(数组,0,false);
您可以将此代码用作转换器,并将字符串
类型替换为某些列表
:
public static String decToBin(int value) {
String result = "";
while (value > 1) {
result += value % 2;
value /= 2;
}
result += value;
result = new StringBuilder(result)
.reverse()
.toString();
return result;
}
这就是你如何让它工作的方法:
public class binaryConverter {
static int nr = 16;
static int max = 0;
static int[] bin;
static int[] array;
static int finalBin;
static boolean foundSolution = false;
public static void main(String[] args) {
maxFinder();
binMaker();
toBinary(array, 0, true);
if (!foundSolution)
toBinary(array, 0, false);
for (int i = array.length - 1; i >= 0; i--)
System.out.print(array[i]);
System.out.println();
}
//finds out how many binary numbers are used in order to decide what length to make the array,
//15 = 1111, 10000 = 16 15<16
private static void maxFinder(){
for(int i = 0, n = 1; i<nr; i++){
if(n>nr){
max = i;
break;
}
n*=2; //n doubles for every i loop, starts with one
}
}
//makes a library of standard to binary (0 = 1, 1 = 2; 2 = 4; 3 = 8...)
private static void binMaker(){
int[] temp = new int[max];
for(int i = 0; i<temp.length; i++){
if(i == 0) temp[i] = 1;
else temp[i]=2*temp[i-1];
}
bin = temp;
array = new int[bin.length];
}
//adds the array together in order to access what number the array currently resembles in binary
private static int sum(int[] ar, int length){
int sum = 0;
for(int i = 0; i<=length; i++) if(ar[i]==1) sum += bin[i];
return sum;
}
//loops until the array becomes the number in binary
private static void toBinary(int[] ar, int i, boolean one){ //i = the current number it's on, eg. 10i01, i is the third slot
if(i==array.length || foundSolution) return; //break if
ar[i] = (one) ? 1:0;
if(sum(ar, i)==nr){ //if the temporary array is the number but in binary ...
array = ar; //turns the static array into the temporary array
String temp = "";
for(int z = 0; z<array.length; z++) temp += array[z];
finalBin = Integer.parseInt(temp); //makes finalBin represent the original number but in binary
foundSolution = true;
return;
}
else{ //else go to the next slot
toBinary(ar, i+1, true);
toBinary(ar, i+1, false);
}
}
}
公共类二进制转换器{
静态整数nr=16;
静态int max=0;
静态int[]bin;
静态int[]数组;
静态int-finalBin;
静态布尔foundSolution=false;
公共静态void main(字符串[]args){
maxFinder();
binMaker();
toBinary(数组,0,true);
如果(!foundSolution)
toBinary(数组,0,false);
对于(int i=array.length-1;i>=0;i--)
系统输出打印(数组[i]);
System.out.println();
}
//找出使用了多少二进制数来决定数组的长度,
//15=1111,10000=16 15尝试调试时发生了什么?数组变为{1,0,0,0}…,也就是说,它进行了一个完整的循环,直到所有的数组都变为1,然后0来计算所需的二进制位数(maxFinder
)你可以用log函数来代替…你必须用递归的方式来做吗?有很多更聪明的解决方案:哦,对了!我忘了用log转换成二进制。谢谢你!不过,这并不是说我必须用递归来做,而是我想提高我的递归技能。这有点复杂,因此你很难理解,也很难正确。找一个更简单的方法!啊,是的。我没有想到。我改变了我的,现在我不再得到0,除了我仍然得到不正确的数字。2给出0,4给出1,6给出11,8给出1。这能实现什么?将整数转换为字符串二进制文本
或二进制列表
like a[0,1,1,0,0]
啊,我明白了。但我的问题不是转换成二进制,而是使用递归转换成二进制。重点是练习递归。