编写一个函数,把任意十进制正整数转换成进制数。要求:在主函数中进行输出,而且不能按逆序输出。提示:把十进制数不断被2除,余数放在一个一维数组中,直到商数为0。
public class TenToTwo { public static void main(String[] args) { System.out.println("十进制转换为二进制结果为:"); getTwo(17); } public static void getTwo(int n){ List<Integer> list = new LinkedList<>(); while (n!=0){ list.add(n%2); n/=2; } Collections.reverse(list); for (Integer integer : list) { System.out.print(integer); } } }
input a decimal number: 15
(15)10= (01111)2