commit
e8b294e3f0
@ -0,0 +1,70 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Transform2158 extends JFrame {
|
||||
private JTextField inputField;
|
||||
private JComboBox<String> fromBaseComboBox;
|
||||
private JComboBox<String> toBaseComboBox;
|
||||
private JTextField resultField;
|
||||
|
||||
public Transform2158() {
|
||||
// 初始化界面
|
||||
setTitle("进制转换工
|
||||
setSize(400, 300);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null); // 居中显示
|
||||
initUI();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
JPanel panel = new JPanel(new GridLayout(4, 2));
|
||||
|
||||
JLabel inputLabel = new JLabel("输入数字:");
|
||||
inputField = new JTextField(20);
|
||||
panel.add(inputLabel);
|
||||
panel.add(inputField);
|
||||
|
||||
JLabel fromBaseLabel = new JLabel("从进制:");
|
||||
String[] bases = {"2", "8", "10", "16"};
|
||||
fromBaseComboBox = new JComboBox<>(bases);
|
||||
panel.add(fromBaseLabel);
|
||||
panel.add(fromBaseComboBox);
|
||||
|
||||
JLabel toBaseLabel = new JLabel("到进制:");
|
||||
toBaseComboBox = new JComboBox<>(bases);
|
||||
panel.add(toBaseLabel);
|
||||
panel.add(toBaseComboBox);
|
||||
|
||||
JButton convertButton = new JButton("转换");
|
||||
convertButton.addActionListener(e -> performConversion());
|
||||
panel.add(convertButton);
|
||||
|
||||
JLabel resultLabel = new JLabel("结果:");
|
||||
resultField = new JTextField(20);
|
||||
resultField.setEditable(false);
|
||||
panel.add(resultLabel);
|
||||
panel.add(resultField);
|
||||
|
||||
add(panel);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private void performConversion() {
|
||||
String input = inputField.getText();
|
||||
int fromBase = Integer.parseInt((String) Objects.requireNonNull(fromBaseComboBox.getSelectedItem()));
|
||||
int toBase = Integer.parseInt((String) Objects.requireNonNull(toBaseComboBox.getSelectedItem()));
|
||||
|
||||
try {
|
||||
int number = Integer.parseInt(input, fromBase);
|
||||
String result = Integer.toString(number, toBase);
|
||||
resultField.setText(result);
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "输入错误,请检查输入格式。", "错误", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(Transform2158::new);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Transform4381 {
|
||||
|
||||
//此代码不实现界面
|
||||
|
||||
public static void main(String[] args) {
|
||||
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();
|
||||
|
||||
try {
|
||||
int number = Integer.parseInt(input, fromBase);
|
||||
String result = Integer.toString(number, toBase);
|
||||
System.out.println("转换结果: " + result);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("输入的数字在指定的进制中无效。");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue