third commit

main
Lsj 1 month ago
parent 15da340f2a
commit ab0ab0bfd3

@ -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);
}
}

@ -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