这个想法是程序会显示数字和它们的二进制等价但是它只显示数字和一个二进制文件,结果是什么错误
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;
}
}
答案 0 :(得分:2)
您可以使用
Integer.toBinaryString(number));
答案 1 :(得分: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));
}
答案 2 :(得分:0)
在这里:
System.out.print("Convert to binary is:");
你在<{1}}循环中不,因此它只打印出数字,只打印最后一个数字的二进制等价物。