项目要求:打印菜单项,让用户输入对应的字母(不区分大小写),则选择了相应的进制转换
接下来接受用户的输入,验证用户输入的合法性,合法则进行进制转换,输出结果
分析:进制转换分为X->十进制 十进制->X进制
X->十进制 :按权展开,权用在原进制基的上标,即几个原进制基相乘再乘以系数
十进制->X进制:除基取余,除以要转换的目标进制的基,倒序取余,直到商为0
2、8、16进制之间的转换,先转换成十进制再转换成目标进制
涉及到一些知识点;原进制转换到目标进制的基数在哪里都需要用到,使用全局变量
整型转字符,输出字符则输出的是字符本身
字符转整型,输出整型则输出字符的ASCII值
判断字符的范围是否在数字字符(0-9)的方法:
str.charAt(i) >= '0' && str.charAt(i) < ‘9’
如果是判断是否在0-9中的某个字符之间的方法是:
int from= 2; char ch = (char) from; 此时ch的值进行运算的值是原原本本的2
而 char ch1= ‘2’ 此时ch1进行运算的值是字符‘2’的ASCII值50
所以用ch+'0'来表示经过转换成的字符的ASCII值
字符转数字核心:字符在进行运算时是用ASCII值进行操作,看原数字(ASCII值)和目标数字的差距
比如让A->10 B->11 C->12 D->13 则通用方法‘char’- 55即可
Java代码:
static int from;//原进制的基
static int to;//目标进制的基
while (true ) {
String str = "";
Scanner scan = new Scanner(System.in);
while (true) {
showMeua();
System.out.println("请选择对应选项");
str = scan.next();
if(str .equals("M") ) {
System.exit(0);
break;
}
Boolean leagal = inputIsLegal(str);
if (leagal) {
break;
}
}
String data = "";
while (true) {
System.out.printf("%d进制->%d进制,请输入要转换的数\n", from, to);
data = scan.next();
Boolean leagal = dataIsLegal(data);
if (leagal) {
break;
}
}
String result = conver(from, to, data);
System.out.printf("%s(%d) ->%s(%d)\n ", data, from, result, to);
}
}
private static String conver(int from, int to, String data) {
if(from == 10)
{
if(to !=16)
{
data= tenConveSmall(from ,to,data ) ;
}
else
{
data = tenConveSixteen(from ,to,data);
}
}else if(to == 10)
{
if(from !=16)
{
data= smallConveTen(from ,to,data ) ;
}
else
{
data = sixteenConveTen(from ,to,data) ;
}
}else
{
if(from !=16)
{
data = smallConveTen(from ,10,data ) ;
if(to !=16)
{
data = tenConveSmall(10,to ,data ) ;
}else
{
data = tenConveSixteen(10,to ,data ) ;
}
}else
{
data = sixteenConveTen(from ,10,data );
if(to!=16)
{
data = tenConveSmall(10,to,data ) ;
}else
{
data = tenConveSixteen(10,to,data ) ;
}
}
}
return data ;
}
private static String sixteenConveTen(int from, int to, String data) {
int i=0,digits = 0;
int len = data .length() ;
String str1 ="";
int[] arr =new int[len ] ;
for( i=0;i<len ;i++) {
if(data .charAt(i)>=65 )
{
arr [i] = data .charAt(i)-55;
}
else
{
arr [i] = Integer.parseInt(data.substring(i,i+1));
}
digits = (int) (digits +(arr [i]*Math .pow(from ,len - i-1)));
}
str1 = String .valueOf(digits );
return str1;
}
private static String tenConveSixteen(int from, int to, String data)
{
int num = Integer .parseInt(data) ;
int digits = num,i=0;
int remainder;
String str ="";
while(!(digits == 0) )
{
remainder = digits % to;
if(remainder>=10)
{
char ch = (char) (remainder+55);
str = ch+str ;
}
else
{
str = remainder +str ;
}
digits /= to;
}
return str;
}
private static String smallConveTen(int from, int to, String data) {
int len = data .length(),digits = 0 ;
for(int i=len ;i>=1;i--)
{
digits = (int) ((Integer.parseInt (data.substring(i-1,i))*Math .pow(from ,len -i) )+digits);
}
String sum = digits +"";
return sum ;
}
private static String tenConveSmall(int from, int to, String data) {
int num = Integer .parseInt(data) ;
int digits = num,i=0;
int remainder ;
String str = "";
while(!(digits == 0) )
{
remainder =digits % to;
str = remainder + str;
digits /= to;
}
return str;
}
private static Boolean dataIsLegal(String str) {
int len = str.length();
char ch = (char) from;
boolean flag = true;
for (int i = 0; i < len; i++) {
if (from < 16) {
if (!(str.charAt(i) >= '0' && str.charAt(i) < (ch+'0'))) {
flag = false;
}
} else {
if (!((str.charAt(i) >='0' && str.charAt(i) <= '9') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'F')))
{
flag = false;
}
}
}
return flag;
}
private static Boolean inputIsLegal(String str) {
char ch = Character.toUpperCase(str .charAt(0) );
switch (ch)
{
case 'A' :from = 2;to = 8;break;
case 'B' :from = 2;to = 10;break;
case 'C' :from = 2;to = 16;break;
case 'D' :from = 8;to = 2;break;
case 'E' :from = 8;to = 10;break;
case 'F' :from = 8;to = 16;break;
case 'G' :from = 10;to = 2;break;
case 'H' :from = 10;to = 8;break;
case 'I' :from = 10;to = 16;break;
case 'J' :from = 16;to = 2;break;
case 'K' :from = 16;to = 8;break;
case 'L' :from = 16;to = 10;break;
default :return false ;
}
return true ;
}
private static void showMeua() {
System.out.println("---*------------------------------------*--");
System.out.println("A:2进制 =》8进制 B:2进制 =》10进制 ");
System.out.println("C:2进制 =》16进制 D:8进制 =》2进制 ");
System.out.println("E:8进制 =》10进制 F:8进制 =》16进制 ");
System.out.println("G:10进制 =》2进制 H:10进制 =》8进制 ");
System.out.println("I:10进制 =》16进制 J:16进制 =》2进制 ");
System.out.println("K:16进制 =》8进制 L:16进制 =》10进制 ");
System.out.println("M:退出");
System.out.println("---*------------------------------------*--");
}