diff --git a/BaseConversion2.java b/BaseConversion2.java index 52b9682..70d27b4 100644 --- a/BaseConversion2.java +++ b/BaseConversion2.java @@ -1,57 +1,50 @@ - 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; + int power = 1; // 初始值为1,对应于base^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 - } + int digitValue = Character.isDigit(digit) + ? digit - '0' + : Character.toUpperCase(digit) - 'A' + 10; // A-F - decimalValue += digitValue * Math.pow(base, power); - power++; + decimalValue += digitValue * power; + power *= base; // 更新权重 } return decimalValue; } + // 将十进制转换为指定进制 public static String convertFromDecimal(int number, int base) { - StringBuilder result = new StringBuilder(); - - if (number == 0) { - return "0"; - } + if (number == 0) return "0"; // 特殊情况处理 + StringBuilder result = new StringBuilder(); while (number > 0) { int remainder = number % base; - if (remainder < 10) { - result.append((char) ('0' + remainder)); - } else { - result.append((char) ('A' + remainder - 10)); - } + result.append(remainder < 10 ? (char) ('0' + remainder) : (char) ('A' + remainder - 10)); number /= base; } - return result.reverse().toString(); + 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(); @@ -64,26 +57,34 @@ public class BaseConversion2 { 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(); + String number = numberField.getText().toUpperCase(); + + // 输入验证 + if (!isValidInput(number, sourceBase)) { + resultLabel.setText("请输入有效的数字!"); + return; + } int decimalValue = convertToDecimal(number, sourceBase); String result = convertFromDecimal(decimalValue, targetBase); resultLabel.setText("转换结果: " + result); } catch (NumberFormatException ex) { - resultLabel.setText("请输入有效的数字和进制!"); + resultLabel.setText("进制应为数字,且范围为 2-16!"); } catch (Exception ex) { resultLabel.setText("转换过程中发生错误: " + ex.getMessage()); } } }); + // 添加组件到框架 frame.add(sourceBaseLabel); frame.add(sourceBaseField); frame.add(targetBaseLabel); @@ -95,4 +96,19 @@ public class BaseConversion2 { frame.setVisible(true); } + + // 检查用户输入的数字是否有效 + private static boolean isValidInput(String number, int base) { + for (char digit : number.toCharArray()) { + int digitValue = Character.isDigit(digit) + ? digit - '0' + : Character.toUpperCase(digit) - 'A' + 10; + + if (digitValue < 0 || digitValue >= base) { + return false; // 数字超出进制范围 + } + } + return true; // 输入有效 + } } +