diff --git a/computer/computer.java b/computer/computer.java index 604be82..a99f38e 100644 --- a/computer/computer.java +++ b/computer/computer.java @@ -1,70 +1,59 @@ import java.util.Scanner; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class computer { - // 将任意R进制数X转换为十进制数 - public static int baseToDecimal(String x, int r) { - int decimalValue = 0; - int length = x.length(); - - for (int i = 0; i < length; i++) { - char digit = x.charAt(length - 1 - i); // 从右往左处理 - int value; - - if (Character.isDigit(digit)) { - value = digit - '0'; // 数字字符 - } else { - value = Character.toUpperCase(digit) - 'A' + 10; // 字母字符 - } - decimalValue += value * Math.pow(r, i); - } - return decimalValue; - } - - // 将十进制数转换为R进制数 - public static String decimalToBase(int num, int r) { - if (num == 0) { - return "0"; - } - - StringBuilder result = new StringBuilder(); - - while (num > 0) { - int remainder = num % r; - if (remainder < 10) { - result.append(remainder); // 数字字符 - } else { - result.append((char) ('A' + (remainder - 10))); // 字母字符 - } - num /= r; - } - - return result.reverse().toString(); // 反转结果 - } - - // 将任意R进制数X从进制R_from转换到进制R_to - public static String convertBase(String x, int rFrom, int rTo) { - int decimalValue = baseToDecimal(x, rFrom); // 转换为十进制 - return decimalToBase(decimalValue, rTo); // 转换为目标进制 - } public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - System.out.print("请输入要转换的进制数(例如:1A):"); - String x = scanner.nextLine(); - - System.out.print("请输入原始进制(2-16):"); - int rFrom = scanner.nextInt(); - - System.out.print("请输入目标进制(2-16):"); - int rTo = scanner.nextInt(); - - // 调用转换函数 - String result = convertBase(x, rFrom, rTo); - - System.out.printf("%s 从 %d 进制转换到 %d 进制的结果是: %s\n", x, rFrom, rTo, result); - - scanner.close(); + JFrame frame = new JFrame("进制转换器"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(500, 500); + 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 actionPerformed(ActionEvent e) { + try { + int sourceBase = Integer.parseInt(sourceBaseField.getText()); + int targetBase = Integer.parseInt(targetBaseField.getText()); + String number = numberField.getText(); + + int decimalValue = convertToDecimal(number, sourceBase); + String result = convertFromDecimal(decimalValue, targetBase); + + resultLabel.setText("转换结果: " + result); + } catch (NumberFormatException ex) { + resultLabel.setText("请输入有效的数字和进制!"); + } catch (Exception ex) { + resultLabel.setText("转换过程中发生错误: " + ex.getMessage()); + } + } + }); + + frame.add(sourceBaseLabel); + frame.add(sourceBaseField); + frame.add(targetBaseLabel); + frame.add(targetBaseField); + frame.add(numberLabel); + frame.add(numberField); + frame.add(convertButton); + frame.add(resultLabel); + + frame.setVisible(true); } }