2017-06-20 118 views
1

我的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); 
     } 
    } 
} 

编辑:我现在已经添加以下行来我的主要: 如果(finalBin = NR!)toBinary(数组,0,FALSE); System.out.println(finalBin); 这是为了确保它可以从0开始。然而,我仍然得到了不正确的答案,因为它给了我偶然的数字看似偶然的回报。

+1

当您尝试调试时发生了什么? – shmosel

+0

该数组变为{1,0,0,0,0} ...,也就是说,它进行了一个完整的循环,直到它们全部变成1秒,然后变为0 –

+0

要计算所需的二进制位数(' maxFinder')你可以使用日志功能,而不是...你是否必须以递归的方式做到这一点?有很多更聪明的解决方案:https://www.cs.indiana.edu/cgi-pub/c211/snake/ – m13r

回答

0

你开始你有一个在首位的二进制总是递归:

toBinary(array, 0, true); 

这样,你永远可以得到一个偶数。偶数在“第一”位(表示“2到0的幂”)处始终为零。

你可以启动递归像这样:

toBinary(array, 0, true); 

if (/* not found a solution */) 
    toBinary(array, 0, false); 
+0

啊,是的。我没有想到这一点。我改变了我的,现在我不再得到0,除了我仍然得到不正确的数字。 2给出0,4给出1,6给出11,8给出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; 
} 
+0

这会实现什么? –

+0

将整数转换为“字符串二进制文本”或“二进制列表”,就像一个'[0,1,1,0,0]' –

+0

啊,我明白了。但我有的问题不是转换为二进制文件,而是使用递归转换为二进制文件。整个过程就是练习递归。 –

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); 
     } 
    } 
}