2013-02-13 66 views
0

所以我正在做一个项目,我必须做从二进制数转换为小数等。 这是我的代码到目前为止,还有一个错误。在一个二进制数字如1101中,假设出来的十进制数字是13,但从代码中出来的数字是11.这个错误发生在所有以一堆1开始并且像单个0开始的二进制数字上。二进制到十进制转换Java代码错误

import java.util.*; // imports everything in java.util 

public class newCalculator{ 

    * Conversion asks the user for a binary number. It converts the input to a decimal number 
    * using arrays and then displays the answer to the screen. 
    */ 

    public static void main (String[]args){ // creates the main method 
    binaryToDecimal(); //calls the user defined method binaryToDecimal 
    } 

    public static void binaryToDecimal() { 
    Scanner scan = new Scanner(System.in); // Creates a new Scanner 
    System.out.println("Input a Binary Number"); // Asks the user to input their number 
    String binary = scan.next(); // Creates a new String that stores the value of the input 
    char[] charArray = binary.toCharArray(); //Create a new Array and implements in the input 
    double answer = 0; // Creates a new double called answer setting it to zero 
    for (double index = 0; index < charArray.length; index++){//For loop 
     if (charArray[(int)index] == '1') {//If statement that allows the binary input to work 
     answer = answer + Math.pow(2.0, index);//Sets the answer with the math class power of 2 
     } 
    } 
    System.out.println(answer);//Prints out the final conversion result 
    /* Test Cases Expected Result Output 
    * 101     5    5 
    * 11     3    3 
    * 1     1    1 
    * 1101    13   11<-- 
    * 111     7    7 
    * 1000001    65   65 
    * 1111    15   15 
    * 1001    9    9 
    * 11101    29   23<-- 
    * 10101    21   21 
    * 
    */ 
    } 

} 
+4

不要使用'Math.pow(...)'任何这一点。在论文中写出你如何解决这个问题以找到你的算法,但同样的,将高功率和不必要的浮点方法调用放在只需要简单代数的东西上。请注意,您过度使用评论是相当分心的。 – 2013-02-13 23:30:21

+1

它已经在这里回答:http://stackoverflow.com/questions/7437987/how-to-convert-binary-string-value-to-decimal – fons 2013-02-13 23:31:48

回答

1

正在计算您的预期结果,就好像从右向左读取二进制字符串一样;但是,您的代码正在从左到右读取二进制字符串。

更改此:

for (double index = 0; index < charArray.length; index++){ 

要这样:

for (double index = charArray.length - 1; index >= 0; index--) { 

你也应该改变使用整数作为索引,这样的:

for (int index = charArray.length - 1; index >= 0; index--) { 
+0

感谢您的帮助! – user2070292 2013-02-13 23:37:04

0

你正在经历你的位错误顺序。你的第一个索引指的是最重要的位,而不是最低位。

您对Math.pow调用应该是

answer = answer + Math.pow(2.0, (charArray.length - index - 1)); 

正如汤姆·利斯已经指出的那样,请使用您的循环指数的整数。

+0

感谢它现在运行! – user2070292 2013-02-13 23:33:06

0

对称是答案。如果你看看所有的测试输入,它们都是对称的,除了1101

你的算法是,与其indexMath.pow你需要使用Math.pow(2.0, charArray.length - i - 1)异常正确的,下面是正确执行(实际上只是小的增量变化)

import java.util.Scanner; 

public class NewCalc { 

    public static void main(String[] args) { 
     binaryToDecimal(); 
    } 

    public static void binaryToDecimal() { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Input a Binary Number"); 
     String binary = scan.next(); 
     char[] charArray = binary.toCharArray(); 
     double answer = 0; 
     for (int i = 0; i < charArray.length; i++) { 
      answer = charArray[i] == '1' 
        ? answer + Math.pow(2.0, charArray.length - i - 1) 
        : answer; 
     } 
     System.out.println(answer); 
    } 
} 
0
package pdaproject; 
import java.util.Scanner; 
public class NewCalc { 

    public static void main(String[] args) { 
     binaryToDecimal(); 
    } 
    // convert to decimal (base 10) 
    public static void binaryToDecimal() { 
     Scanner scan = new Scanner(System.in); 
     System.out.println("Input a Binary Number"); 
     String binary = scan.next(); 
     int answer = 0; 
     // process left to right 
     for (int i = 0; i < binary.length(); i++) { 
      answer = 2 * answer + (binary.charAt(i) == '1' ? 1 : 0); 
     } 
     System.out.println(answer); 
    } 
} 
+0

欢迎来到SO。当回答解决提问者的代码而不是替换它时最好。如果提问者提供的代码是无法解决的,那么解释是有帮助的。 – 2013-06-29 11:35:39

0
public class BinaryToDecimal { 
    static int testcase1=1001; 
    public static void main(String[] args) { 
     BinaryToDecimal test = new BinaryToDecimal(); 
     int result = test.convertBinaryToDecimal(testcase1); 
     System.out.println(result); 
    } 

    //write your code here 
    public int convertBinaryToDecimal(int binary) 
    { 
     int deci = 0; 
     int p=1; 
     int rem = 0; 
     while(binary>0) 
     { 
      rem = binary%10; 
      deci = deci+(rem*p); 
      p = p*2; 
      binary = binary/10; 
     } 

     return deci; 
    } 

}