Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
@ -0,0 +1,29 @@
|
||||
### IntelliJ IDEA ###
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
@ -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,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" 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$/10.10.iml" filepath="$PROJECT_DIR$/10.10.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@ -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="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@ -0,0 +1,66 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class BaseConverter {
|
||||
|
||||
private static final String DIGITS = "0123456789ABCDEF";
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
// 输入源进制,目标进制和数值
|
||||
System.out.print("输入源进制(2-16): ");
|
||||
int sourceBase = scanner.nextInt();
|
||||
System.out.print("输入目标进制(2-16): ");
|
||||
int targetBase = scanner.nextInt();
|
||||
System.out.print("输入源进制的数值: ");
|
||||
String value = scanner.next().toUpperCase();
|
||||
|
||||
// 检查输入是否有效
|
||||
if (sourceBase < 2 || sourceBase > 16 || targetBase < 2 || targetBase > 16) {
|
||||
System.out.println("进制必须在2到16之间");
|
||||
return;
|
||||
}
|
||||
|
||||
// 将源进制数值转换为十进制
|
||||
int decimalValue = toDecimal(value, sourceBase);
|
||||
if (decimalValue == -1) {
|
||||
System.out.println("输入的数值在源进制中无效");
|
||||
return;
|
||||
}
|
||||
|
||||
// 将十进制数转换为目标进制
|
||||
String targetValue = fromDecimal(decimalValue, targetBase);
|
||||
|
||||
// 输出结果
|
||||
System.out.println("目标进制的数值: " + targetValue);
|
||||
}
|
||||
|
||||
// 将任意进制的数值转换为十进制
|
||||
private static int toDecimal(String value, int base) {
|
||||
int decimalValue = 0;
|
||||
for (int i = 0; i < value.length(); i++) {
|
||||
char digit = value.charAt(i);
|
||||
int digitValue = DIGITS.indexOf(digit);
|
||||
if (digitValue < 0 || digitValue >= base) {
|
||||
return -1; // 表示无效输入
|
||||
}
|
||||
decimalValue = decimalValue * base + digitValue;
|
||||
}
|
||||
return decimalValue;
|
||||
}
|
||||
|
||||
// 将十进制数值转换为任意进制
|
||||
private static String fromDecimal(int decimalValue, int base) {
|
||||
if (decimalValue == 0) {
|
||||
return "0";
|
||||
}
|
||||
StringBuilder targetValue = new StringBuilder();
|
||||
while (decimalValue > 0) {
|
||||
int remainder = decimalValue % base;
|
||||
targetValue.insert(0, DIGITS.charAt(remainder));
|
||||
decimalValue /= base;
|
||||
}
|
||||
return targetValue.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue