简体   繁体   English

在java中打印二进制数的十进制等值

[英]print the decimal equivalent of a binary number in java

My code is to print the decimal equivalent of a binary number entered by user. 我的代码是打印用户输入的二进制数的十进制等值。

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a binary integer: ");
        int b=in.nextInt();
        int digits=1;
        int q=b;
        //determine the number of digits
        while(q/10>=1){     
            ++digits;
            q/=10;
        }
        System.out.println(digits);
        int decimal=0;
        int i=0;
        //pick off the binary number's digits and calculate the decimal equivalent
        while(i<=digits-1){
            decimal+=b/Math.pow(10,i)%10*Math.pow(2,i);
            i++;
        }           
        System.out.println(decimal);

    }

}

When I enter 1101, it outputs 13, which is the right answer. 当我输入1101时,它输出13,这是正确的答案。 However, when I test the number 11001, the decimal equivalent is supposed to be 25, but it outputs 26. I try to fix it but can't find where the bug is. 但是,当我测试数字11001时,十进制等值应该是25,但它输出26.我尝试修复它但无法找到bug的位置。 Can you guys help me out? 你能帮助我吗?

The problem is that Math.pow returns a floating-point number, and you're doing floating-point calculations where you think you're doing integer calculations. 问题是Math.pow返回一个浮点数,你正在进行浮点计算,你认为你正在进行整数计算。 When i is 4, and you calculate i是4,你计算

b/Math.pow(10,i)%10*Math.pow(2,i);

the calculation goes like this: 计算如下:

b = 11001
b / Math.pow(10,i) = b / 10000 = 1.1001 (not 1)
1.1001 % 10 = 1.1001
1.1001 * Math.pow(2,i) = 1.1001 * 16 = 17.6016 (not 16)

This is then cast to an (int) when you add it to decimal . 然后在将其添加到decimal时将其强制转换为(int) It truncates the last value to 17, but it's too late. 它将最后一个值截断为17,但为时已晚。

Casting the Math.pow results to an (int) will make it work. Math.pow结果转换为(int)将使其工作。 But this isn't the right approach anyway. 但无论如何,这不是正确的方法。 If you want to learn how to do it yourself instead of using parseInt , it's best to input the number as a String (see my earlier comment), and then you don't have to worry about picking off the bits as decimal digits or powers of 10 at all anyway. 如果你想学习如何自己而不是使用parseInt ,最好将数字作为String输入(参见我之前的评论),然后你不必担心将这些位作为十进制数字或幂来取消无论如何,总共10个。 Even using your approach, instead of Math.pow it would be simpler to keep powerOf10 and powerOf2 integer variables that you modify with powerOf10 *= 10; powerOf2 *= 2; 即使使用您的方法,而不是Math.pow ,使用powerOf10 *= 10; powerOf2 *= 2;来保持powerOf10powerOf2整数变量更简单powerOf10 *= 10; powerOf2 *= 2; powerOf10 *= 10; powerOf2 *= 2; in each loop iteration. 在每个循环迭代中。

Try using: 尝试使用:

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter a binary integer: ");
        int b=in.nextInt();
        int answer = Integer.parseInt(in.nextInt() + "", 2);
        System.out.println("The number is " + answer + ".");
    }
}

2 is for base 2. 2是基数2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM