运用短除法
上图为转换为二进制的过程,转换为x进制就将2换成x。
1 import java.util.Scanner; 2 3 public class Change { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner cin = new Scanner(System.in); 8 System.out.print("输入待转换的数和待转换的进制:"); 9 int OldNum = cin.nextInt();//待转换成10进制的数 10 int x = cin.nextInt();//待转换数的进制 11 int num = ten2x(OldNum, x); 12 System.out.println(num); 13 } 14 15 public static int ten2x(int n, int x) { 16 int num = 0; 17 int k =1; 18 while(n!=0) { 19 int m = n % x; 20 num += m*k; 21 n = n / x; 22 k *= 10; 23 } 24 return num; 25 } 26 }
原文地址:https://www.cnblogs.com/forever-snow/p/9061726.html
时间: 2024-10-31 13:26:36