From 8f220df62d0e1bbb58dab3c8c11a2eced49c5a62 Mon Sep 17 00:00:00 2001 From: han <1048309322@qq.com> Date: Thu, 17 Oct 2024 14:53:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B4=AA=E5=AE=89=E5=A6=AE=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BaseConversion.java | 75 -------------------------------------------- BaseConversion2.java | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 75 deletions(-) create mode 100644 BaseConversion2.java diff --git a/BaseConversion.java b/BaseConversion.java index aa2301d..e69de29 100644 --- a/BaseConversion.java +++ b/BaseConversion.java @@ -1,75 +0,0 @@ -import java.util.Scanner; - -public class BaseConversion { - // 将任意R进制数转换为十进制数 - public static int convertToDecimal(String number, int base) { - int decimalValue = 0; - int power = 0; - - // 从右向左处理字符串 - for (int i = number.length() - 1; i >= 0; i--) { - char digit = number.charAt(i); - int digitValue; - - // 处理数字和字母(十六进制) - if (Character.isDigit(digit)) { - digitValue = digit - '0'; // 0-9 - } else { - digitValue = Character.toUpperCase(digit) - 'A' + 10; // A-F - } - - // 累加到十进制值 - decimalValue += digitValue * Math.pow(base, power); - power++; - } - - return decimalValue; - } - - // 将十进制数转换为任意R进制数 - public static String convertFromDecimal(int number, int base) { - StringBuilder result = new StringBuilder(); - - // 特殊情况:如果number为0,直接返回0 - if (number == 0) { - return "0"; - } - - while (number > 0) { - int remainder = number % base; - if (remainder < 10) { - result.append((char) ('0' + remainder)); // 0-9 - } else { - result.append((char) ('A' + remainder - 10)); // A-F - } - number /= base; - } - - // 反转结果字符串 - return result.reverse().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("请输入目标进制 (2-16): "); - int targetBase = scanner.nextInt(); - scanner.nextLine(); // 消耗换行符 - - // 输入要转换的数字 - System.out.print("请输入要转换的数字: "); - String number = scanner.nextLine(); - - // 进行进制转换 - int decimalValue = convertToDecimal(number, sourceBase); - String result = convertFromDecimal(decimalValue, targetBase); - - // 输出结果 - System.out.println("转换结果: " + result); - } -} - diff --git a/BaseConversion2.java b/BaseConversion2.java new file mode 100644 index 0000000..7e477e3 --- /dev/null +++ b/BaseConversion2.java @@ -0,0 +1,68 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class BaseConversion2 { + public static int convertToDecimal(String number, int base) { + int decimalValue = 0; + int power = 0; + + for (int i = number.length() - 1; i >= 0; i--) { + char digit = number.charAt(i); + int digitValue; + + if (Character.isDigit(digit)) { + digitValue = digit - '0'; + } else { + digitValue = Character.toUpperCase(digit) - 'A' + 10; // A-F + } + + decimalValue += digitValue * Math.pow(base, power); + power++; + } + + return decimalValue; + } + + public static String convertFromDecimal(int number, int base) { + StringBuilder result = new StringBuilder(); + + if (number == 0) { + return "0"; + } + + while (number > 0) { + int remainder = number % base; + if (remainder < 10) { + result.append((char) ('0' + remainder)); + } else { + result.append((char) ('A' + remainder - 10)); + } + number /= base; + } + + return result.reverse().toString(); + } + + public static void main(String[] args) { + JFrame frame = new JFrame("进制转换器"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(400, 300); + frame.setLayout(new GridLayout(5, 2)); + + JLabel sourceBaseLabel = new JLabel("源进制 (2-16):"); + JTextField sourceBaseField = new JTextField(); + + JLabel targetBaseLabel = new JLabel("目标进制 (2-16):"); + JTextField targetBaseField = new JTextField(); + + JLabel numberLabel = new JLabel("要转换的数字:"); + JTextField numberField = new JTextField(); + + JButton convertButton = new JButton("转换"); + JLabel resultLabel = new JLabel("转换结果:"); + + convertButton.addActionListener(new ActionListener() { + @Override + public void actionPerf \ No newline at end of file