代码如下:
package com.java;
import java.util.Scanner;
public class TestTransform {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for ( ; ; ){
System.out.println(“请输入小于2的31次方的正整数:(輸入-1結束程序)”);
int i = sc.nextInt();
if(i==0){
System.out.println(“程序结束!”);
break;
}else if(i < Math.pow(2, 4) && i > 0){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
System.out.println(s);
}else if(i < Math.pow(2, 8) && i >= Math.pow(2, 4)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
System.out.println(s1+s);
}else if(i < Math.pow(2, 12) && i >= Math.pow(2, 8)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
int i2 = i1>>4;
int a2 = i2 & 15;
String s2 = a2<=9 ? a2+”” : (char)(a2-10+’a’)+””;
System.out.println(s2+s1+s);
}else if(i < Math.pow(2, 16) && i >= Math.pow(2, 12)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
int i2 = i1>>4;
int a2 = i2 & 15;
String s2 = a2<=9 ? a2+”” : (char)(a2-10+’a’)+””;
int i3 = i2>>4;
int a3 = i3 & 15;
String s3 = a3<=9 ? a3+”” : (char)(a3-10+’a’)+””;
System.out.println(s3+s2+s1+s);
}else if(i < Math.pow(2, 20) && i >= Math.pow(2, 16)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
int i2 = i1>>4;
int a2 = i2 & 15;
String s2 = a2<=9 ? a2+”” : (char)(a2-10+’a’)+””;
int i3 = i2>>4;
int a3 = i3 & 15;
String s3 = a3<=9 ? a3+”” : (char)(a3-10+’a’)+””;
int i4 = i3>>4;
int a4 = i4 & 15;
String s4 = a4<=9 ? a4+”” : (char)(a4-10+’a’)+””;
System.out.println(s4+s3+s2+s1+s);
}else if(i < Math.pow(2, 24) && i >= Math.pow(2, 20)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
int i2 = i1>>4;
int a2 = i2 & 15;
String s2 = a2<=9 ? a2+”” : (char)(a2-10+’a’)+””;
int i3 = i2>>4;
int a3 = i3 & 15;
String s3 = a3<=9 ? a3+”” : (char)(a3-10+’a’)+””;
int i4 = i3>>4;
int a4 = i4 & 15;
String s4 = a4<=9 ? a4+”” : (char)(a4-10+’a’)+””;
int i5 = i4>>4;
int a5 = i5 & 15;
String s5 = a5<=9 ? a5+”” : (char)(a5-10+’a’)+””;
System.out.println(s5+s4+s3+s2+s1+s);
}else if(i < Math.pow(2, 28) && i >= Math.pow(2, 24)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
int i2 = i1>>4;
int a2 = i2 & 15;
String s2 = a2<=9 ? a2+”” : (char)(a2-10+’a’)+””;
int i3 = i2>>4;
int a3 = i3 & 15;
String s3 = a3<=9 ? a3+”” : (char)(a3-10+’a’)+””;
int i4 = i3>>4;
int a4 = i4 & 15;
String s4 = a4<=9 ? a4+”” : (char)(a4-10+’a’)+””;
int i5 = i4>>4;
int a5 = i5 & 15;
String s5 = a5<=9 ? a5+”” : (char)(a5-10+’a’)+””;
int i6 = i5>>4;
int a6 = i6 & 15;
String s6 = a6<=9 ? a6+”” : (char)(a6-10+’a’)+””;
System.out.println(s6+s5+s4+s3+s2+s1+s);
}else if(i < Math.pow(2, 31) && i >= Math.pow(2, 30)){
int a = i & 15;
String s = a<=9 ? a+”” : (char)(a-10+’a’)+””;
int i1 = i>>4;
int a1 = i1 & 15;
String s1 = a1<=9 ? a1+”” : (char)(a1-10+’a’)+””;
int i2 = i1>>4;
int a2 = i2 & 15;
String s2 = a2<=9 ? a2+”” : (char)(a2-10+’a’)+””; //2 147 483 647 1 073 741 824
int i3 = i2>>4;
int a3 = i3 & 15;
String s3 = a3<=9 ? a3+”” : (char)(a3-10+’a’)+””;
int i4 = i3>>4;
int a4 = i4 & 15;
String s4 = a4<=9 ? a4+”” : (char)(a4-10+’a’)+””;
int i5 = i4>>4;
int a5 = i5 & 15;
String s5 = a5<=9 ? a5+”” : (char)(a5-10+’a’)+””;
int i6 = i5>>4;
int a6 = i6 & 15;
String s6 = a6<=9 ? a6+”” : (char)(a6-10+’a’)+””;
int i7 = i6>>4;
int a7 = i7 & 15;
String s7 = a7<=9 ? a7+”” : (char)(a7-10+’a’)+””;
System.out.println(s7+s6+s5+s4+s3+s2+s1+s);
}
}
}
}
感觉代码太长而且有重复,一定有优化的空间,请各位技术大佬不吝赐教!!