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);