进制转换

main
LIN 1 month ago
parent bd0b555853
commit b3621b4905

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/group.iml" filepath="$PROJECT_DIR$/.idea/group.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,93 @@
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class BaseConverterApp extends Application {
private TextField inputField;
private ComboBox<Integer> fromBaseBox;
private ComboBox<Integer> toBaseBox;
private TextField resultField;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Base Converter");
// 创建网格布局
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
// 创建输入框
Label inputLabel = new Label("Input Number:");
grid.add(inputLabel, 0, 0);
inputField = new TextField();
grid.add(inputField, 1, 0);
// 创建“From Base”下拉菜单
Label fromBaseLabel = new Label("From Base:");
grid.add(fromBaseLabel, 0, 1);
fromBaseBox = new ComboBox<>();
for (int i = 2; i <= 16; i++) {
fromBaseBox.getItems().add(i);
}
fromBaseBox.setValue(2); // 默认二进制
grid.add(fromBaseBox, 1, 1);
// 创建“To Base”下拉菜单
Label toBaseLabel = new Label("To Base:");
grid.add(toBaseLabel, 0, 2);
toBaseBox = new ComboBox<>();
for (int i = 2; i <= 16; i++) {
toBaseBox.getItems().add(i);
}
toBaseBox.setValue(2); // 默认二进制
grid.add(toBaseBox, 1, 2);
// 创建结果框
Label resultLabel = new Label("Result:");
grid.add(resultLabel, 0, 3);
resultField = new TextField();
resultField.setEditable(false); // 结果框为只读
grid.add(resultField, 1, 3);
// 创建转换按钮
Button convertButton = new Button("Convert");
convertButton.setOnAction(e -> convert());
grid.add(convertButton, 0, 4, 2, 1);
// 设置场景并显示
Scene scene = new Scene(grid, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
private void convert() {
try {
// 获取输入的数字和选择的进制
String inputNumber = inputField.getText();
int fromBase = fromBaseBox.getValue();
int toBase = toBaseBox.getValue();
// 将输入的数字从起始进制转换为十进制
int decimalValue = Integer.parseInt(inputNumber, fromBase);
// 将十进制数字转换为目标进制
String result = Integer.toString(decimalValue, toBase).toUpperCase();
// 显示转换结果
resultField.setText(result);
} catch (NumberFormatException ex) {
// 如果输入无效,显示错误信息
resultField.setText("Invalid Input");
}
}
}
Loading…
Cancel
Save