0

这个想法是程序将显示数字及其二进制等价物,但最后只显示数字和一个二进制文件,出了什么问题

public class Convert {

    public static void main(String[] args) {
        int number;
        for (number = 0; number < 4095; number++) {
            System.out.println("Number is :" + number);
        }
        System.out.print("Convert to binary is:");
        System.out.print(binaryform(number));
    }

    private static Object binaryform(int number) {
        int remainder;
        if (number <= 1) {
            System.out.print(number);
            return null;
        }
        remainder = number % 2;
        binaryform(number >> 1);
        System.out.print(remainder); {
        }
        return remainder;
    }
}
4

3 回答 3

2

您可以使用

Integer.toBinaryString(number));
于 2013-05-15T17:17:10.450 回答
1

如下更改

for(number=0;number<4095;number++){

System.out.println("Number is :"+ number);
System.out.print("Convert to binary is:");
System.out.print(binaryform(number));
}
于 2013-05-15T17:15:27.027 回答
0

在这里:

System.out.print("Convert to binary is:");

不在循环中for,所以它只会打印出数字和最后一个数字的二进制等价物。

于 2013-05-15T17:17:13.827 回答