欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

十进制转换为任意进制

程序员文章站 2022-03-21 11:12:13
...

思路:定义一个整数,除以所要换的进制,循环除,保存余数到一个数组,输出

package jinzhi.jinzhi;

public class test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
      test1 test=new test1();
      test.trans(19,8);
      
	}

	private void trans(int num, int k) {
		// TODO Auto-generated method stub
		int remain;
		int[] re=new int[100];
		int location=0;
		while(num!=0){
			remain=num%k;
			num=num/k;
			re[location]=remain;
			location++;
					
		}
		show(re,location);
	}

	private void show(int[] re, int location) {
		// TODO Auto-generated method stub
		for(int i=location-1;i>=0;i--){
			System.out.print(re[i]);
		}
	}

}