JAVA进行进制转换,10进制转换为其他进制

来源:互联网 发布:python 条码 编辑:程序博客网 时间:2024/06/09 21:02
import java.util.*;public class Demo1 {    public static void main(String args[]) {        Scanner sc = new Scanner(System.in);        System.out.println("请输入需要转换的数字:");        int X = sc.nextInt();        System.out.println("请输入需要转换成的进制:");        int Y = sc.nextInt();        int c = 0;        List l = new ArrayList();        System.out.print(X + "转换成" + Y + "进制是:\t");        for (; ; ) {            if (X < Y) {                l.add(X);                break;            }            l.add(X % Y);            X = X / Y;        }        Iterator<Integer> it = l.iterator();        int a[] = new int[l.size()];        while (it.hasNext()) {            a[c] = (int) it.next();            c++;        }        for (int i = 0; i < a.length; i++) {            System.out.print(a[l.size() - i - 1] + "");        }    }}

0 0
原创粉丝点击