From ab0ab0bfd35a4271bf08023270a3d11080c56f17 Mon Sep 17 00:00:00 2001 From: Lsj <1163927332@qq.com> Date: Thu, 17 Oct 2024 14:07:19 +0800 Subject: [PATCH] third commit --- FirstDemo/demo.java | 58 +++++++++-------------------------- FirstDemo/demoController.java | 56 +++++++++++++++++++++++++++++++++ FirstDemo/demoView.fxml | 24 +++++++++++++++ 3 files changed, 95 insertions(+), 43 deletions(-) create mode 100644 FirstDemo/demoController.java create mode 100644 FirstDemo/demoView.fxml diff --git a/FirstDemo/demo.java b/FirstDemo/demo.java index f117ab8..197c2c6 100644 --- a/FirstDemo/demo.java +++ b/FirstDemo/demo.java @@ -1,49 +1,21 @@ package FirstDemo; -import java.util.Scanner; - -public class demo { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - // 输入初始进制,要转换的进制数和目标进制 - System.out.print("请输入初始进制: "); - int sourceBase = scanner.nextInt(); - - System.out.print("请输入要转换的数字: "); - String sourceNumber = scanner.next(); - - System.out.print("请输入目标进制: "); - int targetBase = scanner.nextInt(); - - - int decimalValue = toDecimal(sourceNumber, sourceBase); - String targetNumber = fromDecimal(decimalValue, targetBase); - - System.out.printf("转换成" + targetBase +"进制的结果为: %s\n", targetNumber); - - scanner.close(); - } - - // 将输入转换为10进制 - public static int toDecimal(String number, int base) { - return Integer.parseInt(number, base); +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class demo extends Application { + + @Override + public void start(Stage primaryStage) throws Exception { + FXMLLoader loader = new FXMLLoader(getClass().getResource("demoView.fxml")); + primaryStage.setTitle("进制转换"); + primaryStage.setScene(new Scene(loader.load(), 400, 300)); // 调整高度 + primaryStage.show(); } - //将转换后的10进制数转换为R进制数 - public static String fromDecimal(int number, int base) { - StringBuilder sb = new StringBuilder(); - while (number > 0) { - int remainder = number % base; - // 对于大于9的数字,用字母A-F表示 - if (remainder >= 10) { - sb.append((char) ('A' + remainder - 10)); - } else { - sb.append(remainder); - } - number /= base; - } - return sb.reverse().toString(); //反转字符串以得到正确的顺序 + public static void main(String[] args) { + launch(args); } - } \ No newline at end of file diff --git a/FirstDemo/demoController.java b/FirstDemo/demoController.java new file mode 100644 index 0000000..c8eddab --- /dev/null +++ b/FirstDemo/demoController.java @@ -0,0 +1,56 @@ +package FirstDemo; + +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; + +public class demoController { + + @FXML + private TextField initialBaseField; + @FXML + private TextField numberToConvertField; + @FXML + private TextField targetBaseField; + @FXML + private Label resultLabel; + + @FXML + private void convert() { + try { + int sourceBase = Integer.parseInt(initialBaseField.getText()); + String sourceNumber = numberToConvertField.getText(); + int targetBase = Integer.parseInt(targetBaseField.getText()); + + int decimalValue = toDecimal(sourceNumber, sourceBase); + String targetNumber = fromDecimal(decimalValue, targetBase); + + resultLabel.setText("转换成 " + targetBase + " 进制的结果为: " + targetNumber); + } catch (NumberFormatException e) { + resultLabel.setText("输入无效,请确保输入有效的进制和数字。"); + } catch (Exception e) { + resultLabel.setText("发生错误:" + e.getMessage()); + } + } + + // 将输入转换为10进制 + public static int toDecimal(String number, int base) { + return Integer.parseInt(number, base); + } + + // 将转换后的10进制数转换为目标进制数 + public static String fromDecimal(int number, int base) { + StringBuilder sb = new StringBuilder(); + while (number > 0) { + int remainder = number % base; + // 对于大于9的数字,用字母A-F表示 + if (remainder >= 10) { + sb.append((char) ('A' + remainder - 10)); + } else { + sb.append(remainder); + } + number /= base; + } + return sb.reverse().toString(); // 反转字符串以得到正确的顺序 + } +} \ No newline at end of file diff --git a/FirstDemo/demoView.fxml b/FirstDemo/demoView.fxml new file mode 100644 index 0000000..9d97346 --- /dev/null +++ b/FirstDemo/demoView.fxml @@ -0,0 +1,24 @@ + + + + + + + + +