思路:不断对N%2,将结果保存到一个字符串中,再对N/2后继续循环
最后逆置字符串
import java.util.*;
/**
* @author hardneedl
*/
public class IntegerToBinDemo {
private interface Convertor{
R convert(T t)throws RuntimeException;
}
private static class IntBinConvertor implements Convertor{
public String convert(Integer integer) throws RuntimeException {
return Integer.toBinaryString(integer);
}
}
public static void main(String... args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入一个整数:");
System.out.println(new IntBinConvertor().convert(scanner.nextInt()));
}
}