Compare commits

...

1 Commits
main ... fuk

Author SHA1 Message Date
陈朝阳 9103c2e9d5 czy
4 months ago

@ -0,0 +1,59 @@
package czy.czy;
import java.math.BigInteger;
import java.util.Scanner;
public class BaseConverter {
/**
*
*
* @param numStr
* @param fromBase
* @param toBase
* @return
*/
public static String convertBase(String numStr, int fromBase, int toBase) {
// 验证进制是否合法
if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) {
throw new IllegalArgumentException("Base must be between 2 and 16.");
}
// 将原始进制的数转换为十进制
BigInteger decimalValue = new BigInteger(numStr, fromBase);
// 如果目标进制为10则直接返回
if (toBase == 10) {
return decimalValue.toString();
}
// 将十进制数转换为目标进制
return decimalValue.toString(toBase).toUpperCase(); // 转换为大写以便更好识别十六进制字符
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取原始进制
System.out.print("请输入原始进制 (2-16): ");
int fromBase = scanner.nextInt();
// 读取目标进制
System.out.print("请输入目标进制 (2-16): ");
int toBase = scanner.nextInt();
// 读取原始数值
System.out.print("请输入原始数值: ");
String numStr = scanner.next();
// 调用转换方法
try {
String result = convertBase(numStr, fromBase, toBase);
System.out.println("转换后的结果为: " + result);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
scanner.close();
}
}

@ -0,0 +1,89 @@
package czy.czy;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.math.BigInteger;
public class BaseConverterApp extends Application {
private TextField fromBaseField;
private TextField toBaseField;
private TextField numStrField;
private Label resultLabel;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("进制转换器");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(8);
grid.setHgap(10);
Label fromBaseLabel = new Label("原始进制:");
fromBaseField = new TextField();
fromBaseField.setPromptText("2-16");
GridPane.setConstraints(fromBaseLabel, 0, 0);
GridPane.setConstraints(fromBaseField, 1, 0);
Label toBaseLabel = new Label("目标进制:");
toBaseField = new TextField();
toBaseField.setPromptText("2-16");
GridPane.setConstraints(toBaseLabel, 0, 1);
GridPane.setConstraints(toBaseField, 1, 1);
Label numStrLabel = new Label("原始数值:");
numStrField = new TextField();
GridPane.setConstraints(numStrLabel, 0, 2);
GridPane.setConstraints(numStrField, 1, 2);
Button convertButton = new Button("转换");
GridPane.setConstraints(convertButton, 1, 3);
resultLabel = new Label();
GridPane.setConstraints(resultLabel, 1, 4);
grid.getChildren().addAll(fromBaseLabel, fromBaseField, toBaseLabel, toBaseField, numStrLabel, numStrField, convertButton, resultLabel);
Scene scene = new Scene(grid, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
convertButton.setOnAction(event -> {
try {
int fromBase = Integer.parseInt(fromBaseField.getText());
int toBase = Integer.parseInt(toBaseField.getText());
String numStr = numStrField.getText();
String result = conv(numStr, fromBase, toBase);
resultLabel.setText("转换后的结果为: " + result);
} catch (IllegalArgumentException e) {
resultLabel.setText(e.getMessage());
}
});
}
public static void main(String[] args) {
launch(args);
}
public static String conv(String numStr, int fromBase, int toBase) {
if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) {
throw new IllegalArgumentException("Base must be between 2 and 16.");
}
BigInteger decimalValue = new BigInteger(numStr, fromBase);
if (toBase == 10) {
return decimalValue.toString();
}
return decimalValue.toString(toBase).toUpperCase();
}
}
Loading…
Cancel
Save