third commit

main
Lsj 1 month ago
parent 15da340f2a
commit ab0ab0bfd3

@ -1,49 +1,21 @@
package FirstDemo; package FirstDemo;
import java.util.Scanner; import javafx.application.Application;
import javafx.fxml.FXMLLoader;
public class demo { import javafx.scene.Scene;
public static void main(String[] args) { import javafx.stage.Stage;
Scanner scanner = new Scanner(System.in);
public class demo extends Application {
// 输入初始进制,要转换的进制数和目标进制
System.out.print("请输入初始进制: "); @Override
int sourceBase = scanner.nextInt(); public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("demoView.fxml"));
System.out.print("请输入要转换的数字: "); primaryStage.setTitle("进制转换");
String sourceNumber = scanner.next(); primaryStage.setScene(new Scene(loader.load(), 400, 300)); // 调整高度
primaryStage.show();
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);
} }
//将转换后的10进制数转换为R进制数 public static void main(String[] args) {
public static String fromDecimal(int number, int base) { launch(args);
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(); //反转字符串以得到正确的顺序
} }
} }

@ -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(); // 反转字符串以得到正确的顺序
}
}

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="FirstDemo.demoController">
<children>
<Label text="初始进制:" layoutX="30.0" layoutY="30.0"/>
<TextField fx:id="initialBaseField" layoutX="150.0" layoutY="30.0"/>
<Label text="要转换的数字:" layoutX="30.0" layoutY="80.0"/>
<TextField fx:id="numberToConvertField" layoutX="150.0" layoutY="80.0"/>
<Label text="目标进制:" layoutX="30.0" layoutY="130.0"/>
<TextField fx:id="targetBaseField" layoutX="150.0" layoutY="130.0"/>
<Label fx:id="resultLabel" layoutX="30.0" layoutY="180.0" prefWidth="300.0" wrapText="true" textAlignment="CENTER"/>
<Button text="转换" layoutX="80.0" layoutY="240.0"
prefWidth="240.0" prefHeight="45.0"
onAction="#convert"/>
</children>
</AnchorPane>
Loading…
Cancel
Save