一个 Java 控制台程序,无需使用 for 循环使用预定义方法即可将"BINARY"转换为"DECIMAL"



我编写了一个java控制台程序,它打印输入的二进制值的十进制值。现在,我的程序有一个问题

例如输出:输入input = 10011101,那么二进制值是3534200而不是157

在网上查找二进制值到十进制值的转换公式后,这是我编写这个程序的参考。

1 * 2 * 2 ^ 6 ^ 7 + 0 + 0 * 2 ^ 5 + 1 * 2 ^ 4 + 1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0 = 157。

我尝试了长版本在制作这个程序,在使用for循环…我猜这是个愚蠢的挑战?哈哈!

下面是代码(没有注释!):

byte binary[] = new byte[127];   //declared a byte array for input value
int power = 2, formula = 0;      //declared the power and the formula as int
System.out.print("Enter Binary: "); //prints "Enter binary:"
System.in.read(binary);          //inserts the input in the binary array
Integer bin = Integer.parseInt(new String(binary).trim()); //converts the input value to int for another conversion
String b = Integer.toString(bin); //converts the int bin to string
Integer num = Integer.parseInt(new String(b.substring(b.length() - 1).trim())); 
//num variable gets the value of last string (should be anyway)
System.out.println("The Decimal Value of " + bin + " is ");
for(int i = b.length(); i > 0; i--){ 
    for(int a = i; a > 0; a--){ //the condition
        power = power*2;        //as the loop goes, if 2^3 then it should be 8
    }
    formula = formula + (num * (power)); //as the given formula above, this is what I did
    System.out.println("power: " + power);//if you want reference, I left it here
    System.out.println("formula: " +formula);
    System.out.println("num: " + num);
    num = Integer.parseInt(new String(b.substring(i - 1).trim())); //uhm dunno how to describe this, but you'll see
    power = 2;
}
System.out.print(formula);

}}

自从我开始使用java,这是我唯一知道的事情。(参考我自9月4日以来的最后一个问题)

请帮忙:

你有很多问题:

1) Integer.parseInt(new String(binary).trim())并不像你想象的那样。因此,您的num是错误的。

你算错了你的能量。

一般的建议是,用一些空行把你的代码分成有意义的小块。这样眼睛会舒服些。

固定的程序应该是这样的:

public static void main(String[] args) throws IOException {
    byte binary[] = new byte[127];   //declared a byte array for input value
    int power = 2, formula = 0;      //declared the power and the formula as int
    System.out.print("Enter Binary: "); //prints "Enter binary:"
    System.in.read(binary);          //inserts the input in the binary array
    Integer bin = Integer.parseInt(new String(binary).trim()); //converts the input value to int for another conversion
    String b = Integer.toString(bin); //converts the int bin to string
    Integer num; //num variable gets the value of last string (should be anyway)
    System.out.println("The Decimal Value of " + bin + " is ");
    for(int i = b.length(); i > 0; i--){ 
        power = 1;
        num = Integer.parseInt(Character.toString(b.charAt(i-1)));
        for(int a = i; a < b.length(); a++){ //the condition
            power = power*2;        //as the loop goes, if 2^3 then it should be 8
        }
        formula = formula + (num * (power)); //as the given formula above, this is what I did
        System.out.println("power: " + power);//if you want reference, I left it here
        System.out.println("formula: " +formula);
        System.out.println("num: " + num);
    }
    System.out.print(formula);
}

最新更新