我的Java程序存在问题。我创建了一个程序,它接受一个整数并将其转换为二进制值。当数字是奇数时,没有任何问题。 15转换为1111,17转换为10001,依此类推。当数字是偶数时,就会出现问题。如果我输入16,18,20等,它只返回0.每次。另外,重要的是要注意我通过使用递归方法得到我的号码,一旦它到达它的号码就会停止。
这是我的代码。感谢我能得到的任何帮助,即使它没有解决问题。
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);
}
}
}
编辑:我现在已将以下行添加到我的主页: if(finalBin!= nr)toBinary(array,0,false); 的System.out.println(finalBin); 这是为了确保它可以从0开始。但是,我仍然得到不正确的答案,因为它给我看起来偶数回报偶数。
答案 0 :(得分:0)
你开始递归时总是在二进制文件的第一位有一个:
toBinary(array, 0, true);
这样你永远不会得到偶数。偶数在“第一”位始终为零(表示“2为0的幂”)。
您可以像这样开始递归:
toBinary(array, 0, true);
if (/* not found a solution */)
toBinary(array, 0, false);
答案 1 :(得分:0)
您可以将此代码用作转换器,并将String
类型替换为某些List
:
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;
}
答案 2 :(得分:0)
这就是你如何让它发挥作用:
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);
}
}
}