diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3312e67 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Git-test.iml b/Git-test.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Git-test.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..fe7aa2b --- /dev/null +++ b/src/Main.java @@ -0,0 +1,15 @@ +//TIP 要运行代码,请按 或 +// 点击装订区域中的 图标。 +public class Main { + public static void main(String[] args) { + //TIP 当文本光标位于高亮显示的文本处时按 + // 查看 IntelliJ IDEA 建议如何修正。 + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP 按 开始调试代码。我们已经设置了一个 断点 + // 但您始终可以通过按 添加更多断点。 + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/src/Transform2158.java b/src/Transform2158.java new file mode 100644 index 0000000..a7f68ad --- /dev/null +++ b/src/Transform2158.java @@ -0,0 +1,99 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Scanner; + +public class Transform2158{ + + public static String convertBase(String input, int fromBase, int toBase) { + try { + int number = Integer.parseInt(input, fromBase); + return Integer.toString(number, toBase); + } catch (NumberFormatException e) { + return null; + } + } + + public static boolean isValidBase(int base) { + return base == 2 || base == 8 || base == 10 || base == 16; + } + + public static void main(String[] args) { + if (args.length > 0 && args[0].equals("cli")) { + runCLI(); + } else { + SwingUtilities.invokeLater(() -> new Transform2158().createAndShowGUI()); + } + } + + private static void runCLI() { + Scanner scanner = new Scanner(System.in); + System.out.print("请输入输入数字: "); + String input = scanner.nextLine(); + + System.out.print("请输入从进制 (2, 8, 10, 16): "); + int fromBase = scanner.nextInt(); + + System.out.print("请输入到进制 (2, 8, 10, 16): "); + int toBase = scanner.nextInt(); + + if (isValidBase(fromBase) && isValidBase(toBase)) { + String result = convertBase(input, fromBase, toBase); + if (result != null) { + System.out.println("转换结果: " + result); + } else { + System.out.println("输入的数字在指定的进制中无效。"); + } + } else { + System.out.println("无效的进制,请输入 (2, 8, 10, 16)。"); + } + + scanner.close(); + } + + private void createAndShowGUI() { + JFrame frame = new JFrame("进制转换器"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(300, 200); + frame.setLayout(null); + + JButton button = new JButton("开始转换"); + button.setBounds(80, 70, 140, 30); + button.addActionListener(new ConvertAction()); + frame.add(button); + + frame.setVisible(true); + } + + private static class ConvertAction implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + String input = JOptionPane.showInputDialog("请输入输入数字:"); + if (input == null) return; + + String fromBaseStr = JOptionPane.showInputDialog("请输入从进制 (2, 8, 10, 16):"); + if (fromBaseStr == null) return; + + String toBaseStr = JOptionPane.showInputDialog("请输入到进制 (2, 8, 10, 16):"); + if (toBaseStr == null) return; + + try { + int fromBase = Integer.parseInt(fromBaseStr); + int toBase = Integer.parseInt(toBaseStr); + + if (isValidBase(fromBase) && isValidBase(toBase)) { + String result = convertBase(input, fromBase, toBase); + if (result != null) { + JOptionPane.showMessageDialog(null, "转换结果: " + result); + } else { + JOptionPane.showMessageDialog(null, "输入的数字在指定的进制中无效。"); + } + } else { + JOptionPane.showMessageDialog(null, "无效的进制,请输入 (2, 8, 10, 16)。"); + } + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(null, "输入的数字在指定的进制中无效。"); + } + } + } +} \ No newline at end of file