十进制转二进制
(十进制转二进制)(循环)输入一个十进制的数
输出二进制 入门小白,不知道有没有bug{:10_250:}
num = int(input("please input the number: "))
ans = []
while num != 0:
n = num % 2
ans.append(n)
num = int(num / 2)
ans.reverse()
print(ans)
参考代码:
import java.util.Scanner;
public class BaseConversion {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print(">>> ");
int number = scan.nextInt();
String result = transform(number);
System.out.println(result);
}
public static String transform(int number){
StringBuilder result = new StringBuilder();
while (number != 0){
result.append(number % 2);
number /= 2;
}
result = result.reverse();
return "0b" + result;
}
} Integer.toBinaryString(num);
页:
[1]