选择进制

master
王家彬 1 month ago
parent 3109505073
commit daba5fa502

@ -12,59 +12,59 @@ public class test2 extends Application {
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
// 创建VBox作为垂直布局容器 // 创建VBox作为垂直布局容器
VBox vBox = new VBox(10.0); VBox vBox = new VBox(10.0);
// 创建提示标签 // 创建提示标签
Label promptLabel = new Label("请输入一个十进制数"); Label promptLabel = new Label("请输入一个");
promptLabel.setStyle("-fx-font-weight: bold;"); promptLabel.setStyle("-fx-font-weight: bold;");
// 创建输入框并设置提示文本 // 创建输入框并设置提示文本
TextField inputField = new TextField(); TextField inputField = new TextField();
inputField.setPromptText("输入数字"); inputField.setPromptText("输入数字");
// 创建输入框并设置提示文本
TextField fromBaseField = new TextField();
fromBaseField.setPromptText("输入原始进制");
// 创建输入框并设置提示文本
TextField toBaseField = new TextField();
toBaseField.setPromptText("输入目标进制");
// 创建标签用于显示转换结果 // 创建标签用于显示转换结果
Label binaryLabel = new Label(); Label resultLabel = new Label();
Label octalLabel = new Label();
Label hexLabel = new Label();
Label base6Label = new Label();
// 创建按钮 // 创建按钮
Button binaryButton = new Button("二进制"); Button convertButton = new Button("转换");
Button octalButton = new Button("八进制");
Button hexButton = new Button("十六进制");
Button base6Button = new Button("六进制");
// 更新标签的函数 // 更新标签的函数
final TextField finalInputField = inputField; final TextField finalInputField = inputField;
Button[] buttons = {binaryButton, octalButton, hexButton, base6Button}; final TextField finalFromBaseField = fromBaseField;
Label[] labels = {binaryLabel, octalLabel, hexLabel, base6Label}; final TextField finalToBaseField = toBaseField;
for (int i = 0; i < buttons.length; i++) {
final int index = i; convertButton.setOnAction(e -> {
buttons[i].setOnAction(e -> { try {
try { String inputText = finalInputField.getText();
String inputText = finalInputField.getText(); int fromBase = Integer.parseInt(finalFromBaseField.getText());
int number = Integer.parseInt(inputText); int toBase = Integer.parseInt(finalToBaseField.getText());
switch (index) {
case 0: // 将输入的数字从原始进制转换为十进制
labels[index].setText(String.format("二进制: %s", Integer.toBinaryString(number).toUpperCase())); int decimalNumber = Integer.parseInt(inputText, fromBase);
break;
case 1: // 将十进制数字转换为目标进制
labels[index].setText(String.format("八进制: %s", Integer.toOctalString(number))); String result = toBaseString(decimalNumber, toBase);
break;
case 2: resultLabel.setText(String.format("%d 进制: %s", toBase, result));
labels[index].setText(String.format("十六进制: %s", Integer.toHexString(number).toUpperCase())); } catch (NumberFormatException ex) {
break; Alert alert = new Alert(Alert.AlertType.ERROR);
case 3: alert.setTitle("输入错误");
labels[index].setText(String.format("六进制: %s", toBase6String(number))); alert.setHeaderText("无效输入");
break; alert.setContentText("请输入有效的数字和进制。");
} alert.showAndWait();
} catch (NumberFormatException ex) { }
Alert alert = new Alert(Alert.AlertType.ERROR); });
alert.setTitle("输入错误");
alert.setHeaderText("无效输入");
alert.setContentText("请输入一个有效的数字。");
alert.showAndWait();
}
});
}
// 将组件添加到布局中 // 将组件添加到布局中
vBox.getChildren().addAll(promptLabel, inputField, binaryButton, octalButton, hexButton, base6Button, binaryLabel, octalLabel, hexLabel, base6Label); vBox.getChildren().addAll(promptLabel, inputField, fromBaseField, toBaseField, convertButton, resultLabel);
// 设置场景和舞台 // 设置场景和舞台
Scene scene = new Scene(vBox, 300.0, 200.0); Scene scene = new Scene(vBox, 300.0, 200.0);
primaryStage.setTitle("进制转换器"); primaryStage.setTitle("进制转换器");
@ -72,20 +72,20 @@ public class test2 extends Application {
primaryStage.show(); primaryStage.show();
} }
// 将十进制数转换为六进制字符串的方法 // 将十进制数转换为指定进制的字符串
private String toBase6String(int number) { private String toBaseString(int number, int base) {
if (number == 0) { if (number == 0) {
return "0"; return "0";
} else { } else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
char[] digits = new char[]{'0', '1', '2', '3', '4', '5'}; char[] digits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
boolean isNegative = number < 0; boolean isNegative = number < 0;
if (isNegative) { if (isNegative) {
number = -number; number = -number;
} }
while (number > 0) { while (number > 0) {
sb.insert(0, digits[number % 6]); sb.insert(0, digits[number % base]);
number /= 6; number /= base;
} }
if (isNegative) { if (isNegative) {
sb.insert(0, '-'); sb.insert(0, '-');
@ -97,4 +97,4 @@ public class test2 extends Application {
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
} }

Loading…
Cancel
Save