1、数值的计算
2、进制之间的转换
3、代码实现
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要转换的数字");
int num1 = scanner.nextInt();
System.out.println("请输入需要转换的进制");
int num2 = scanner.nextInt();
System.out.println(num1+"转换的为"+num2+"进制的数为:");
System.out.println(numberSystem(num1,num2));
scanner.close();
}
public static String numberSystem(int num,int k){
String s = "";
int m = 0;
while(num>=k){
m = num%k;
num = num/k;
if(m>9) s = s+(char)('A'+m-10);
else s=s+m;
}
return stringReverse(s+num);
}
public static String stringReverse(String s){
char [] arr = s.toCharArray();
int left = 0;
int right = s.length()-1;
while(left<right){
char temp = arr[right];
arr[right] = arr[left];
arr[left] = temp;
left++;
right--;
}
String str = new String(arr);
return str;
}
}