From 3756cfbdf032a4ecacc08a2e5d85e8c76383284a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AE=B6=E5=BD=AC?= <1317640527@qq.com> Date: Thu, 10 Oct 2024 18:44:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=AD=E8=BF=9B=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- text1.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/text1.java b/text1.java index 815850e..f70bc2b 100644 --- a/text1.java +++ b/text1.java @@ -5,18 +5,38 @@ import javafx.scene.layout.VBox; import javafx.stage.Stage; public class text1 extends Application { - + private String toBase6String(int number) { + if (number == 0) { + return "0"; + } + StringBuilder sb = new StringBuilder(); + char[] digits = {'0', '1', '2', '3', '4', '5'}; + boolean isNegative = number < 0; + if (isNegative) { + number = -number; + } + while (number > 0) { + sb.insert(0, digits[number % 6]); + number /= 6; + } + if (isNegative) { + sb.insert(0, '-'); + } + return sb.toString(); + } @Override public void start(Stage primaryStage) { // 创建文本输入框 TextField inputField = new TextField(); inputField.setPromptText("输入一个十进制数"); - + VBox vBox = new VBox(10); // 创建标签显示结果 Label binaryLabel = new Label(); Label octalLabel = new Label(); Label hexLabel = new Label(); +// + // 创建按钮 Button convertButton = new Button("转换"); convertButton.setOnAction(e -> { @@ -25,6 +45,9 @@ public class text1 extends Application { binaryLabel.setText(String.format("二进制: %s", Integer.toBinaryString(number).toUpperCase())); octalLabel.setText(String.format("八进制: %s", Integer.toOctalString(number))); hexLabel.setText(String.format("十六进制: %s", Integer.toHexString(number).toUpperCase())); + Label base6Label = new Label(); + base6Label.setText(String.format("六进制: %s", toBase6String(number))); + vBox.getChildren().add(base6Label); } catch (NumberFormatException ex) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("输入错误"); @@ -35,7 +58,7 @@ public class text1 extends Application { }); // 创建布局 - VBox vBox = new VBox(10); + vBox.getChildren().addAll(inputField, convertButton, binaryLabel, octalLabel, hexLabel); // 设置场景