|
|
|
@ -1,69 +1,82 @@
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
public class jinzhizhuanhuan {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将任意进制数转换为十进制数
|
|
|
|
|
public static int toDecimal(String number, int base) {
|
|
|
|
|
int decimal = 0;
|
|
|
|
|
int power = 0;
|
|
|
|
|
int len = number.length();
|
|
|
|
|
|
|
|
|
|
for (int i = len - 1; i >= 0; i--) {
|
|
|
|
|
char digit = number.charAt(i);
|
|
|
|
|
int value = Character.digit(digit, base);
|
|
|
|
|
if (value == -1) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid digit for base " + base + ": " + digit);
|
|
|
|
|
}
|
|
|
|
|
decimal += value * Math.pow(base, power);
|
|
|
|
|
power++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decimal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将十进制数转换为任意进制数
|
|
|
|
|
public static String fromDecimal(int number, int base) {
|
|
|
|
|
if (number == 0) {
|
|
|
|
|
return "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
char[] digits = "0123456789ABCDEF".toCharArray();
|
|
|
|
|
|
|
|
|
|
while (number > 0) {
|
|
|
|
|
int remainder = number % base;
|
|
|
|
|
result.insert(0, digits[remainder]);
|
|
|
|
|
number /= base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
|
|
|
|
|
// 输入原始进制和数
|
|
|
|
|
System.out.print("请输入原始进制(2-16): ");
|
|
|
|
|
int sourceBase = scanner.nextInt();
|
|
|
|
|
System.out.print("请输入原始数: ");
|
|
|
|
|
String sourceNumber = scanner.next();
|
|
|
|
|
|
|
|
|
|
// 输入目标进制
|
|
|
|
|
System.out.print("请输入目标进制(2-16): ");
|
|
|
|
|
int targetBase = scanner.nextInt();
|
|
|
|
|
|
|
|
|
|
// 转换并输出结果
|
|
|
|
|
try {
|
|
|
|
|
int decimal = toDecimal(sourceNumber, sourceBase);
|
|
|
|
|
String targetNumber = fromDecimal(decimal, targetBase);
|
|
|
|
|
System.out.println("转换后的数是: " + targetNumber);
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
System.out.println(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scanner.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
commit 800d62e6d558ef19f046ea0d70ed2c0dd74374d9
|
|
|
|
|
Author: 214115093 <214115093@qq.com>
|
|
|
|
|
Date: Thu Oct 10 14:46:20 2024 +0800
|
|
|
|
|
|
|
|
|
|
zhuanhuan
|
|
|
|
|
|
|
|
|
|
diff --git a/jinzhizhuanhuan.java b/jinzhizhuanhuan.java
|
|
|
|
|
new file mode 100644
|
|
|
|
|
index 0000000..10c0651
|
|
|
|
|
--- /dev/null
|
|
|
|
|
+++ b/jinzhizhuanhuan.java
|
|
|
|
|
@@ -0,0 +1,69 @@
|
|
|
|
|
+import java.util.Scanner;
|
|
|
|
|
+public class jinzhizhuanhuan {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 将任意进制数转换为十进制数
|
|
|
|
|
+ public static int toDecimal(String number, int base) {
|
|
|
|
|
+ int decimal = 0;
|
|
|
|
|
+ int power = 0;
|
|
|
|
|
+ int len = number.length();
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = len - 1; i >= 0; i--) {
|
|
|
|
|
+ char digit = number.charAt(i);
|
|
|
|
|
+ int value = Character.digit(digit, base);
|
|
|
|
|
+ if (value == -1) {
|
|
|
|
|
+ throw new IllegalArgumentException("Invalid digit for base " + base + ": " + digit);
|
|
|
|
|
+ }
|
|
|
|
|
+ decimal += value * Math.pow(base, power);
|
|
|
|
|
+ power++;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return decimal;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 将十进制数转换为任意进制数
|
|
|
|
|
+ public static String fromDecimal(int number, int base) {
|
|
|
|
|
+ if (number == 0) {
|
|
|
|
|
+ return "0";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
|
+ char[] digits = "0123456789ABCDEF".toCharArray();
|
|
|
|
|
+
|
|
|
|
|
+ while (number > 0) {
|
|
|
|
|
+ int remainder = number % base;
|
|
|
|
|
+ result.insert(0, digits[remainder]);
|
|
|
|
|
+ number /= base;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return result.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
|
|
+
|
|
|
|
|
+ // 输入原始进制和数
|
|
|
|
|
+ System.out.print("请输入原始进制(2-16): ");
|
|
|
|
|
+ int sourceBase = scanner.nextInt();
|
|
|
|
|
+ System.out.print("请输入原始数: ");
|
|
|
|
|
+ String sourceNumber = scanner.next();
|
|
|
|
|
+
|
|
|
|
|
+ // 输入目标进制
|
|
|
|
|
+ System.out.print("请输入目标进制(2-16): ");
|
|
|
|
|
+ int targetBase = scanner.nextInt();
|
|
|
|
|
+
|
|
|
|
|
+ // 转换并输出结果
|
|
|
|
|
+ try {
|
|
|
|
|
+ int decimal = toDecimal(sourceNumber, sourceBase);
|
|
|
|
|
+ String targetNumber = fromDecimal(decimal, targetBase);
|
|
|
|
|
+ System.out.println("转换后的数是: " + targetNumber);
|
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
|
+ System.out.println(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ scanner.close();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
\ No newline at end of file
|
|
|
|
|