Java中的printf方法不支持直接输出二进制数。如果需要输出二进制数,可以通过以下方法实现:
int num = 10;
String binaryString = Integer.toBinaryString(num);
System.out.printf("Binary representation of %d is %s%n", num, binaryString);
public static String toBinaryString(int num) {
StringBuilder sb = new StringBuilder();
while (num > 0) {
sb.insert(0, num % 2);
num /= 2;
}
return sb.toString();
}
int num = 10;
String binaryString = toBinaryString(num);
System.out.printf("Binary representation of %d is %s%n", num, binaryString);