|
|
@ -55,4 +55,27 @@ public class BaseConverter {
|
|
|
|
int decimalValue = toDecimal(str, sourceBase);
|
|
|
|
int decimalValue = toDecimal(str, sourceBase);
|
|
|
|
return fromDecimal(decimalValue, targetBase);
|
|
|
|
return fromDecimal(decimalValue, targetBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 主方法,用于测试
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取用户输入
|
|
|
|
|
|
|
|
System.out.print("请输入数据(例如 A1): ");
|
|
|
|
|
|
|
|
String input = scanner.nextLine().toUpperCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入源进制数(例如 16): ");
|
|
|
|
|
|
|
|
int sourceBase = scanner.nextInt();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("请输入目标进制数(例如 2): ");
|
|
|
|
|
|
|
|
int targetBase = scanner.nextInt();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 进制转换
|
|
|
|
|
|
|
|
String result = convertBase(input, sourceBase, targetBase);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("转换结果:" + result); // 输出转换结果
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scanner.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|