蒋蒸 1 month ago
parent d28060e831
commit 975c07c894

@ -2,6 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/computer/computer.iml" filepath="$PROJECT_DIR$/computer/computer.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/computer1.iml" filepath="$PROJECT_DIR$/.idea/computer1.iml" />
</modules>
</component>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +1,6 @@
import java.util.Scanner;
public class computer {
public class computer{
// 将任意进制数转换为十进制数
public static int toDecimal(String number, int base) {
@ -33,35 +33,6 @@ public class computer {
return decimal;
}
// 将十进制数转换为任意进制数
public static String fromDecimal(int decimal, int base) {
if (base < 2 || base > 16) {
throw new IllegalArgumentException("Base must be between 2 and 16");
}
StringBuilder number = new StringBuilder();
// 当十进制数大于0时持续转换
while (decimal > 0) {
int remainder = decimal % base;
char digit;
// 处理0-9
if (remainder < 10) {
digit = (char) ('0' + remainder);
}
// 处理10-15A-F
else {
digit = (char) ('A' + (remainder - 10));
}
number.insert(0, digit); // 在字符串的开头插入新的字符
decimal /= base; // 更新十进制数,去掉已转换的部分
}
// 如果转换后的字符串为空即输入的十进制数为0则返回"0"
return number.length() == 0 ? "0" : number.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Loading…
Cancel
Save