From 975c07c894c3fc064c06e0a59effeb58c1d040af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E8=92=B8?= <3505869163@qq.com> Date: Thu, 10 Oct 2024 15:33:28 +0800 Subject: [PATCH] V0 --- .idea/modules.xml | 1 + computer/computer.iml | 11 +++++++++ computer.java => computer/computer.java | 31 +------------------------ 3 files changed, 13 insertions(+), 30 deletions(-) create mode 100644 computer/computer.iml rename computer.java => computer/computer.java (63%) diff --git a/.idea/modules.xml b/.idea/modules.xml index b4476f8..69f62ca 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/computer/computer.iml b/computer/computer.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/computer/computer.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/computer.java b/computer/computer.java similarity index 63% rename from computer.java rename to computer/computer.java index c6dfb8f..9e840ae 100644 --- a/computer.java +++ b/computer/computer.java @@ -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-15(A-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);