From d6507e6b7494d6153f18e32a5daee302f6a583d1 Mon Sep 17 00:00:00 2001 From: sjb <3151867900@qq.com> Date: Wed, 16 Oct 2024 10:41:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BaseConversion.java | 58 --------------- BaseConversionGUI.java | 93 ++++++++++++++++++++++++ out/production/jzq/BaseConversion.class | Bin 2282 -> 0 bytes 3 files changed, 93 insertions(+), 58 deletions(-) delete mode 100644 BaseConversion.java create mode 100644 BaseConversionGUI.java delete mode 100644 out/production/jzq/BaseConversion.class diff --git a/BaseConversion.java b/BaseConversion.java deleted file mode 100644 index 49894d2..0000000 --- a/BaseConversion.java +++ /dev/null @@ -1,58 +0,0 @@ -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/BaseConversionGUI.java b/BaseConversionGUI.java new file mode 100644 index 0000000..baf9c69 --- /dev/null +++ b/BaseConversionGUI.java @@ -0,0 +1,93 @@ +import java.util.Scanner; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class BaseConversionGUI extends JFrame implements ActionListener { + + private JTextField inputField; + private JTextField fromBaseField; + private JTextField toBaseField; + private JLabel resultLabel; + + public BaseConversionGUI() { + setTitle("进制转换器"); + setSize(400, 200); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); // 居中显示窗口 + + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(4, 2)); + + panel.add(new JLabel("请输入要转换的数字: ")); + inputField = new JTextField(); + panel.add(inputField); + + panel.add(new JLabel("请输入源进制 (2-16): ")); + fromBaseField = new JTextField(); + panel.add(fromBaseField); + + panel.add(new JLabel("请输入目标进制 (2-16): ")); + toBaseField = new JTextField(); + panel.add(toBaseField); + + JButton convertButton = new JButton("转换"); + convertButton.addActionListener(this); + panel.add(convertButton); + + resultLabel = new JLabel(); + panel.add(resultLabel); + + add(panel); + } + + @Override + public void actionPerformed(ActionEvent e) { + String numStr = inputField.getText(); + int fromBase = Integer.parseInt(fromBaseField.getText()); + int toBase = Integer.parseInt(toBaseField.getText()); + + if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) { + JOptionPane.showMessageDialog(this, "进制必须在 2 到 16 之间。", "错误", JOptionPane.ERROR_MESSAGE); + } else { + String result = convertBase(numStr, fromBase, toBase); + resultLabel.setText(numStr + " 从 " + fromBase + " 进制转换到 " + toBase + " 进制的结果是: " + result); + } + } + + public static void main(String[] args) { + EventQueue.invokeLater(() -> { + BaseConversionGUI gui = new BaseConversionGUI(); + gui.setVisible(true); + }); + } + + // 原有的转换方法保持不变 + public static String convertBase(String numStr, int fromBase, int toBase) { + int decimalNumber = convertToDecimal(numStr, fromBase); + return convertFromDecimal(decimalNumber, toBase); + } + + public static int convertToDecimal(String numStr, int base) { + return Integer.parseInt(numStr, base); + } + + 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))); + } + num /= base; + } + return result.reverse().toString(); + } +} diff --git a/out/production/jzq/BaseConversion.class b/out/production/jzq/BaseConversion.class deleted file mode 100644 index db7ab2e4f0fc10d6006c376802985f0ad8432384..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2282 zcmaJ>Yjaa&5PnW>CrwXjnv&iF0R_|2QVLk5rBzx)BQ2oC0;0lcdMJVPB<6ykBI?M< z=;$yag98jBc)<@Sm2py}BYf6h;CE>$f5BnIXU_?w(v0oQo85hP_kH%+-SfvkH*Nx0 zi(@`4#&bSw!t-9dpd#vnOLP}rlw-3T19A-dun1e^*s5Y%8D7H6K4=&cX}cG%_%Mte zV%h1%F8O`ci``zlCid4Q>@{mD^U zD+l7Hu_ZS?Vx+h0BMDjp1Id`47}nEq(F;abc7Hshz(3HZXN>-&dB{j-;z_fcrkJH= zwu&Lefp? z`pq{d7WLp%J z7v7aB6+}waQ5u4GACcNoo@~+lzT*9z23OgBxb zu-cB!W#ftVA=}ukqK5;cVxI!vP%@W}8JptL%W6SVY}ZO*T|*d4RE%lZj|k~rrJ(lF zZWKt;5XS*I4r)kXT!V?Eh9?lw(1w)?s;s!VNK~XWyot0tlF^VwPC;n)>g4RrQ`5)4 zoBjTS+25|re0J&1*^g#UPfY)KrYo%CkcPK#Si=z{6*N9{p1E;y_SU)S6Zvptbz4Vg zb5~fy+jvL8qK7Vb&iyp=^~aBRDOhS-rf(g;dvR*|{14&P;pr0-;f~Jm?O#8=d*+wF z-v3a;yY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