新增功能

main
陈朝阳 4 months ago
parent 2a631a121b
commit d258995714

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/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_X" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/elt.iml" filepath="$PROJECT_DIR$/.idea/elt.iml" />
<module fileurl="file://$PROJECT_DIR$/text1/text1.iml" filepath="$PROJECT_DIR$/text1/text1.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,59 @@
package src;
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 src;
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();
}
}

@ -0,0 +1,17 @@
package src;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}

@ -0,0 +1,11 @@
<?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$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Loading…
Cancel
Save