parent
80f1e0531d
commit
e7785e58df
@ -1,41 +0,0 @@
|
|||||||
package jzzh;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class BaseConverter {
|
|
||||||
// 将任意 R 进制的数转换为十进制
|
|
||||||
public static int convertToDecimal(String number, int base) {
|
|
||||||
return Integer.parseInt(number, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将十进制数转换为任意目标进制
|
|
||||||
public static String convertFromDecimal(int decimalNumber, int targetBase) {
|
|
||||||
return Integer.toString(decimalNumber, targetBase).toUpperCase(); // 使用大写表示结果
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
// 输入原始进制
|
|
||||||
System.out.print("请输入原始进制 (2 ≤ R ≤ 16): ");
|
|
||||||
int originalBase = scanner.nextInt();
|
|
||||||
|
|
||||||
// 输入需要转换的数
|
|
||||||
System.out.print("请输入进制数: ");
|
|
||||||
String number = scanner.next().toUpperCase(); // 防止字母大小写混淆
|
|
||||||
|
|
||||||
// 输入目标进制
|
|
||||||
System.out.print("请输入目标进制 (2 ≤ R ≤ 16): ");
|
|
||||||
int targetBase = scanner.nextInt();
|
|
||||||
|
|
||||||
// 将原始 R 进制数转换为十进制
|
|
||||||
int decimalNumber = convertToDecimal(number, originalBase);
|
|
||||||
|
|
||||||
// 将十进制数转换为目标 R' 进制
|
|
||||||
String result = convertFromDecimal(decimalNumber, targetBase);
|
|
||||||
|
|
||||||
// 输出结果
|
|
||||||
System.out.println("转换后的结果: " + result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -0,0 +1,103 @@
|
|||||||
|
package jzzh;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
|
// 将任意 R 进制的数转换为十进制
|
||||||
|
public static int convertToDecimal(String number, int base) {
|
||||||
|
return Integer.parseInt(number, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将十进制数转换为任意目标进制
|
||||||
|
public static String convertFromDecimal(int decimalNumber, int targetBase) {
|
||||||
|
return Integer.toString(decimalNumber, targetBase).toUpperCase(); // 使用大写表示结果
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage primaryStage) {
|
||||||
|
primaryStage.setTitle("进制转换器");
|
||||||
|
|
||||||
|
// 创建布局
|
||||||
|
GridPane grid = new GridPane();
|
||||||
|
grid.setPadding(new Insets(10));
|
||||||
|
grid.setVgap(8);
|
||||||
|
grid.setHgap(10);
|
||||||
|
|
||||||
|
// 原始进制输入
|
||||||
|
Label originalBaseLabel = new Label("原始进制 (2-16):");
|
||||||
|
grid.add(originalBaseLabel, 0, 0);
|
||||||
|
TextField originalBaseInput = new TextField();
|
||||||
|
grid.add(originalBaseInput, 1, 0);
|
||||||
|
|
||||||
|
// 要转换的数输入
|
||||||
|
Label numberLabel = new Label("要转换的数:");
|
||||||
|
grid.add(numberLabel, 0, 1);
|
||||||
|
TextField numberInput = new TextField();
|
||||||
|
grid.add(numberInput, 1, 1);
|
||||||
|
|
||||||
|
// 目标进制输入
|
||||||
|
Label targetBaseLabel = new Label("目标进制 (2-16):");
|
||||||
|
grid.add(targetBaseLabel, 0, 2);
|
||||||
|
TextField targetBaseInput = new TextField();
|
||||||
|
grid.add(targetBaseInput, 1, 2);
|
||||||
|
|
||||||
|
// 转换结果显示
|
||||||
|
Label resultLabel = new Label("转换结果:");
|
||||||
|
grid.add(resultLabel, 0, 3);
|
||||||
|
TextField resultOutput = new TextField();
|
||||||
|
resultOutput.setEditable(false);
|
||||||
|
grid.add(resultOutput, 1, 3);
|
||||||
|
|
||||||
|
// 转换按钮
|
||||||
|
Button convertButton = new Button("转换");
|
||||||
|
grid.add(convertButton, 1, 4);
|
||||||
|
|
||||||
|
// 按钮事件处理
|
||||||
|
convertButton.setOnAction(e -> {
|
||||||
|
try {
|
||||||
|
int originalBase = Integer.parseInt(originalBaseInput.getText());
|
||||||
|
String number = numberInput.getText().toUpperCase();
|
||||||
|
int targetBase = Integer.parseInt(targetBaseInput.getText());
|
||||||
|
|
||||||
|
// 检查进制范围
|
||||||
|
if (originalBase < 2 || originalBase > 16 || targetBase < 2 || targetBase > 16) {
|
||||||
|
showAlert("错误", "进制应在2到16之间。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 进行转换
|
||||||
|
int decimalNumber = convertToDecimal(number, originalBase);
|
||||||
|
String result = convertFromDecimal(decimalNumber, targetBase);
|
||||||
|
resultOutput.setText(result);
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
showAlert("错误", "请输入有效的数字和进制。");
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
showAlert("错误", "输入的数不符合原始进制。");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 创建场景并显示
|
||||||
|
Scene scene = new Scene(grid, 400, 250);
|
||||||
|
primaryStage.setScene(scene);
|
||||||
|
primaryStage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 弹出提示框
|
||||||
|
private void showAlert(String title, String message) {
|
||||||
|
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||||
|
alert.setTitle(title);
|
||||||
|
alert.setHeaderText(null);
|
||||||
|
alert.setContentText(message);
|
||||||
|
alert.showAndWait();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue