diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/jzq.iml b/.idea/jzq.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/jzq.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3ceb08f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BaseConversion.java b/BaseConversion.java new file mode 100644 index 0000000..49894d2 --- /dev/null +++ b/BaseConversion.java @@ -0,0 +1,58 @@ +import java.util.Scanner; + +public class BaseConversion { + + // 将任意R进制数转换为十进制数 + public static int convertToDecimal(String numStr, int base) { + return Integer.parseInt(numStr, base); + } + + // 将十进制数转换为任意R进制数 + public static String convertFromDecimal(int num, int base) { + if (num == 0) { + return "0"; + } + StringBuilder result = new StringBuilder(); + while (num > 0) { + int remainder = num % base; + if (remainder < 10) { + result.append(remainder); + } else { + result.append((char) ('A' + (remainder - 10))); // A-F 对应 10-15 + } + num /= base; + } + return result.reverse().toString(); // 反转结果 + } + + // 将一个R进制数转换为另一个R进制数 + public static String convertBase(String numStr, int fromBase, int toBase) { + int decimalNumber = convertToDecimal(numStr, fromBase); + return convertFromDecimal(decimalNumber, toBase); + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("请输入要转换的数字: "); + String numStr = scanner.nextLine(); + + System.out.print("请输入源进制 (2-16): "); + int fromBase = scanner.nextInt(); + + System.out.print("请输入目标进制 (2-16): "); + int toBase = scanner.nextInt(); + + // 验证进制范围 + if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) { + System.out.println("进制必须在 2 到 16 之间。"); + } else { + String result = convertBase(numStr, fromBase, toBase); + System.out.println(numStr + " 从 " + fromBase + " 进制转换到 " + toBase + " 进制的结果是: " + result); + } + + scanner.close(); + + //This is a test + } +} diff --git a/out/production/jzq/.idea/.gitignore b/out/production/jzq/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/out/production/jzq/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/out/production/jzq/.idea/jzq.iml b/out/production/jzq/.idea/jzq.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/out/production/jzq/.idea/jzq.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/jzq/.idea/misc.xml b/out/production/jzq/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/out/production/jzq/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/jzq/.idea/modules.xml b/out/production/jzq/.idea/modules.xml new file mode 100644 index 0000000..3ceb08f --- /dev/null +++ b/out/production/jzq/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/jzq/.idea/vcs.xml b/out/production/jzq/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/jzq/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/jzq/BaseConversion.class b/out/production/jzq/BaseConversion.class new file mode 100644 index 0000000..db7ab2e Binary files /dev/null and b/out/production/jzq/BaseConversion.class differ diff --git a/out/production/jzq/README.md b/out/production/jzq/README.md new file mode 100644 index 0000000..e390538 --- /dev/null +++ b/out/production/jzq/README.md @@ -0,0 +1,2 @@ +# jzq +