# 使用JAVA将非负十进制整数n转换成b进制,递归方法与非递归方法

1.非递归

2.递归
①num/b==0 result.append(num%b)
②num/b!=0 transform(result,num/b,b).append(sb);

1.非递归实现

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
System.out.println("请输入一个十进制数：");
Scanner sc = new Scanner(System.in);
String num = sc.next();
System.out.println("请输入要转化的进制(2-16)：");
String b = sc.next();
if(judge(num)){
System.out.println("输入十进制数格式正确");
}
else{
System.out.println("输入十进制数格式错误");
}
if(judge(b)){
System.out.println("输入转化的进制数格式正确");
}
else{
System.out.println("输入转化的进制数格式错误");
}

String result = transform(num,b);
System.out.println("十进制数 "+num+" 转化为"+b+"进制数为： "+result);
sc.close();

}

public static boolean judge(String num){
char[] numArray = num.toCharArray();
for(int i=0;i<numArray.length;i++){
if(numArray[i]<48 | numArray[i]>57){	//若输入的字符非数字字符，返回false
return false;
}
}
return true;
}

public static String transform(int num,int b){
StringBuffer sb = new StringBuffer();
while(num!=0){
switch(num%b){
case 10: sb.append("A"); break;
case 11: sb.append("B"); break;
case 12: sb.append("C"); break;
case 13: sb.append("D"); break;
case 14: sb.append("E"); break;
case 15: sb.append("F"); break;
default : sb.append(Integer.toString(num%b));
}
num = num/b;
}

return sb.reverse().toString();

}

public static String transform(String numString,String bString){
int num = Integer.parseInt(numString);
int b = Integer.parseInt(bString);
return transform(num,b);
}
}


2.递归实现

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
System.out.println("请输入一个十进制数：");
Scanner sc = new Scanner(System.in);
String num = sc.next();
System.out.println("请输入要转化的进制(2-16)：");
String b = sc.next();
if(judge(num)){
System.out.println("输入十进制数格式正确");
}
else{
System.out.println("输入十进制数格式错误");
}
if(judge(b)){
System.out.println("输入转化的进制数格式正确");
}
else{
System.out.println("输入转化的进制数格式错误");
}

StringBuffer result = new StringBuffer("");
result= transform(result,num,b).reverse();
System.out.println("十进制数 "+num+" 转化为"+b+"进制数为： "+result);
sc.close();

}

public static boolean judge(String num){
char[] numArray = num.toCharArray();
for(int i=0;i<numArray.length;i++){
if(numArray[i]<48 | numArray[i]>57){	//若输入的字符非数字字符，返回false
return false;
}
}
return true;
}
//将十进制数转化为b进制数
public static StringBuffer transform(StringBuffer result,int num,int b){
StringBuffer sb = new StringBuffer();
if(num/b==0){
return result.append(num%b);
}
else{	//处理字符串result，给末尾加上余数
switch(num%b){
case 10: sb.append("A"); break;
case 11: sb.append("B"); break;
case 12: sb.append("C"); break;
case 13: sb.append("D"); break;
case 14: sb.append("E"); break;
case 15: sb.append("F"); break;
default : sb.append(num%b); break;
}
return transform(result,num/b,b).append(sb);
}
}

public static StringBuffer transform(StringBuffer result,String numString,String bString){
int num = Integer.parseInt(numString);
int b = Integer.parseInt(bString);
return transform(result,num,b);
}
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客