陈昱杉新增功能2次

main
Cys 1 month ago
parent 6a2d913333
commit ecc5d99038

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,4 @@
<changelist name="在变基之前未提交的更改_[更改]" date="1728550317435" recycled="true" deleted="true">
<option name="PATH" value="$PROJECT_DIR$/.idea/shelf/在变基之前未提交的更改_[更改]/shelved.patch" />
<option name="DESCRIPTION" value="在变基之前未提交的更改 [更改]" />
</changelist>

@ -0,0 +1,102 @@
Index: Git.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Git.java b/Git.java
new file mode 100644
--- /dev/null (date 1728549940950)
+++ b/Git.java (date 1728549940950)
@@ -0,0 +1,92 @@
+import java.util.Scanner;
+
+public class Git {
+
+ // 将任意进制字符串转换为十进制整数
+ public static int toDecimal(String number, int base) {
+ int decimal = 0;
+ int power = 0;
+
+ // 从字符串的末尾开始处理
+ for (int i = number.length() - 1; i >= 0; i--) {
+ char digit = number.charAt(i);
+
+ // 如果是数字字符0-9
+ if (Character.isDigit(digit)) {
+ int value = Character.getNumericValue(digit);
+ decimal += value * Math.pow(base, power);
+ }
+ // 如果是字母字符A-F 或 a-f表示10-15
+ else if (Character.isLetter(digit)) {
+ char upperDigit = Character.toUpperCase(digit);
+ int value = upperDigit - 'A' + 10;
+ decimal += value * Math.pow(base, power);
+ } else {
+ throw new IllegalArgumentException("Invalid character in number: " + digit);
+ }
+
+ power++;
+ }
+
+ return decimal;
+ }
+
+ // 将十进制整数转换为任意进制字符串
+ public static String fromDecimal(int number, int base) {
+ if (number == 0) {
+ return "0";
+ }
+
+ StringBuilder result = new StringBuilder();
+ char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制
+
+ boolean isNegative = number < 0;
+ if (isNegative) {
+ number = -number;
+ }
+
+ while (number > 0) {
+ int remainder = number % base;
+ result.insert(0, digits[remainder]);
+ number /= base;
+ }
+
+ if (isNegative) {
+ result.insert(0, '-');
+ }
+
+ return result.toString();
+ }
+
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+
+ System.out.print("请输入源进制2-16");
+ int sourceBase = scanner.nextInt();
+ scanner.nextLine(); // 消耗掉换行符
+
+ System.out.print("请输入目标进制2-16");
+ int targetBase = scanner.nextInt();
+ scanner.nextLine(); // 消耗掉换行符
+
+ System.out.print("请输入要转换的" + sourceBase + "进制数字:");
+ String number = scanner.nextLine().trim(); // 读取实际的数字输入
+
+ // 验证源进制数字是否有效
+ for (char c : number.toCharArray()) {
+ if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
+ throw new IllegalArgumentException("Invalid character in source base number: " + c);
+ }
+ }
+
+ // 将源进制数字转换为十进制
+ int decimalNumber = toDecimal(number, sourceBase);
+
+ // 将十进制数字转换为目标进制
+ String convertedNumber = fromDecimal(decimalNumber, targetBase);
+
+ System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber);
+
+ scanner.close();
+ }
+}

@ -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,196 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="0be0f291-b20e-4aa7-ab84-516f82a4b347" name="更改" comment="陈昱杉新增功能2">
<change beforePath="$PROJECT_DIR$/Git.java" beforeDir="false" afterPath="$PROJECT_DIR$/Git.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/uml10_10.java" beforeDir="false" afterPath="$PROJECT_DIR$/uml10_10.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<excluded-from-favorite>
<branch-storage>
<map>
<entry type="LOCAL">
<value>
<list>
<branch-info repo="$PROJECT_DIR$" source="main" />
</list>
</value>
</entry>
</map>
</branch-storage>
</excluded-from-favorite>
<option name="RECENT_BRANCH_BY_REPOSITORY">
<map>
<entry key="$PROJECT_DIR$" value="master" />
</map>
</option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 0
}</component>
<component name="ProjectId" id="2nErgmdri0iejhyqdiwjDMyMhe8" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main",
"kotlin-language-version-configured": "true",
"last_opened_file_path": "C:/Users/陈昱杉/IdeaProjects/cys_cyh_UML",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"project.structure.last.edited": "项目",
"project.structure.proportion": "0.15",
"project.structure.side.proportion": "0.0",
"vue.rearranger.settings.migration": "true",
"应用程序.Git.executor": "Run"
}
}]]></component>
<component name="RunManager" selected="应用程序.uml10_10">
<configuration name="BaseConverterGUI" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="BaseConverterGUI" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Git" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="ALTERNATIVE_JRE_PATH" value="1.8" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="Git" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="uml10_10" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="ALTERNATIVE_JRE_PATH" value="BUNDLED" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="uml10_10" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="JetRunConfigurationType">
<module name="cys_cyh_UML" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
<module name="cys_cyh_UML" />
<option name="filePath" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="应用程序.uml10_10" />
<item itemvalue="应用程序.BaseConverterGUI" />
<item itemvalue="应用程序.Git" />
<item itemvalue="应用程序.Git" />
</list>
</recent_temporary>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="jdk-1.8.0_402-corretto-1.8.0_402-f644763e9732-af3fa2b8" />
</set>
</attachedChunks>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="0be0f291-b20e-4aa7-ab84-516f82a4b347" name="更改" comment="" />
<created>1728549189282</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1728549189282</updated>
<workItem from="1728549190344" duration="4090000" />
<workItem from="1729152242324" duration="382000" />
<workItem from="1729152727145" duration="1355000" />
<workItem from="1729154878735" duration="524000" />
</task>
<task id="LOCAL-00001" summary="Cys">
<option name="closed" value="true" />
<created>1728550814326</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1728550814326</updated>
</task>
<task id="LOCAL-00002" summary="Cys2">
<option name="closed" value="true" />
<created>1728551218182</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1728551218182</updated>
</task>
<task id="LOCAL-00003" summary="陈昱杉新增功能">
<option name="closed" value="true" />
<created>1728551752659</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1728551752659</updated>
</task>
<task id="LOCAL-00004" summary="陈昱杉新增功能2">
<option name="closed" value="true" />
<created>1728552598440</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1728552598440</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Cys" />
<MESSAGE value="Cys2" />
<MESSAGE value="陈昱杉新增功能" />
<MESSAGE value="陈昱杉新增功能2" />
<option name="LAST_COMMIT_MESSAGE" value="陈昱杉新增功能2" />
</component>
</project>

@ -1,22 +1,24 @@
import java.util.Scanner; import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Git { public class Git {
// 将任意进制字符串转换为十进制整数 // 将任意进制字符串转换为十进制整数
public static int toDecimal(String number, int base) { public static int toDecimal(String number, int base) {
int decimal = 0; int decimal = 0;
int power = 0; int power = 0;
// 从字符串的末尾开始处理
// 从字符串的末尾开始处理
for (int i = number.length() - 1; i >= 0; i--) { for (int i = number.length() - 1; i >= 0; i--) {
char digit = number.charAt(i); char digit = number.charAt(i);
// 如果是数字字符0-9 // 如果是数字字符0-9
if (Character.isDigit(digit)) { if (Character.isDigit(digit)) {
int value = Character.getNumericValue(digit); int value = Character.getNumericValue(digit);
decimal += value * Math.pow(base, power); decimal += value * Math.pow(base, power);
} }
// 如果是字母字符A-F 或 a-f表示10-15 // 如果是字母字符A-F 或 a-f表示10-15
else if (Character.isLetter(digit)) { else if (Character.isLetter(digit)) {
char upperDigit = Character.toUpperCase(digit); char upperDigit = Character.toUpperCase(digit);
int value = upperDigit - 'A' + 10; int value = upperDigit - 'A' + 10;
@ -31,14 +33,14 @@ public class Git {
return decimal; return decimal;
} }
// 将十进制整数转换为任意进制字符串 // 将十进制整数转换为任意进制字符串
public static String fromDecimal(int number, int base) { public static String fromDecimal(int number, int base) {
if (number == 0) { if (number == 0) {
return "0"; return "0";
} }
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制
boolean isNegative = number < 0; boolean isNegative = number < 0;
if (isNegative) { if (isNegative) {
@ -59,34 +61,67 @@ public class Git {
} }
public static void main(String[] args) { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建窗口
JFrame frame = new JFrame("进制转换器");
System.out.print("请输入源进制2-16"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int sourceBase = scanner.nextInt(); frame.setSize(400, 300);
scanner.nextLine(); // 消耗掉换行符 frame.setLayout(new GridLayout(5, 2)); // 网格布局
System.out.print("请输入目标进制2-16"); // 创建组件
int targetBase = scanner.nextInt(); JLabel sourceBaseLabel = new JLabel("源进制2-16");
scanner.nextLine(); // 消耗掉换行符 JTextField sourceBaseField = new JTextField();
System.out.print("请输入要转换的" + sourceBase + "进制数字:"); JLabel targetBaseLabel = new JLabel("目标进制2-16");
String number = scanner.nextLine().trim(); // 读取实际的数字输入 JTextField targetBaseField = new JTextField();
// 验证源进制数字是否有效 JLabel numberLabel = new JLabel("要转换的数字:");
for (char c : number.toCharArray()) { JTextField numberField = new JTextField();
if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
throw new IllegalArgumentException("Invalid character in source base number: " + c); JButton convertButton = new JButton("转换");
JLabel resultLabel = new JLabel("转换结果:");
// 添加组件到窗口
frame.add(sourceBaseLabel);
frame.add(sourceBaseField);
frame.add(targetBaseLabel);
frame.add(targetBaseField);
frame.add(numberLabel);
frame.add(numberField);
frame.add(convertButton);
frame.add(resultLabel);
// 添加按钮事件处理
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int sourceBase = Integer.parseInt(sourceBaseField.getText().trim());
int targetBase = Integer.parseInt(targetBaseField.getText().trim());
String number = numberField.getText().trim();
// 验证源进制数字是否有效
for (char c : number.toCharArray()) {
if (!Character.isDigit(c) &&
!(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
throw new IllegalArgumentException("Invalid character in source base number: " + c);
}
}
// 将源进制数字转换为十进制
int decimalNumber = toDecimal(number, sourceBase);
// 将十进制数字转换为目标进制
String convertedNumber = fromDecimal(decimalNumber, targetBase);
// 显示结果
resultLabel.setText("转换后的" + targetBase + "进制数字是:" + convertedNumber);
} catch (NumberFormatException | IllegalArgumentException ex) {
JOptionPane.showMessageDialog(frame, "错误: " + ex.getMessage(), "输入错误", JOptionPane.ERROR_MESSAGE);
}
} }
} });
// 将源进制数字转换为十进制
int decimalNumber = toDecimal(number, sourceBase);
// 将十进制数字转换为目标进制
String convertedNumber = fromDecimal(decimalNumber, targetBase);
System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber);
scanner.close(); // 显示窗口
frame.setVisible(true);
} }
} }

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="0be0f291-b20e-4aa7-ab84-516f82a4b347" name="更改" comment="">
<change afterPath="$PROJECT_DIR$/Git.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 0
}]]></component>
<component name="ProjectId" id="2nErgmdri0iejhyqdiwjDMyMhe8" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main",
"kotlin-language-version-configured": "true",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
<component name="RunManager">
<configuration name="Git" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Git" />
<module name="cys_cyh_UML" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="应用程序.Git" />
</list>
</recent_temporary>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="jdk-1.8.0_402-corretto-1.8.0_402-f644763e9732-af3fa2b8" />
</set>
</attachedChunks>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="0be0f291-b20e-4aa7-ab84-516f82a4b347" name="更改" comment="" />
<created>1728549189282</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1728549189282</updated>
<workItem from="1728549190344" duration="493000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
</project>

@ -1,22 +1,20 @@
import java.util.Scanner; import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class uml10_10 { public class uml10_10 {
// 将任意进制字符串转换为十进制整数
public static int toDecimal(String number, int base) { public static int toDecimal(String number, int base) {
int decimal = 0; int decimal = 0;
int power = 0; int power = 0;
//还好还好 // 从字符串的末尾开始处理
// 从字符串的末尾开始处理
for (int i = number.length() - 1; i >= 0; i--) { for (int i = number.length() - 1; i >= 0; i--) {
char digit = number.charAt(i); char digit = number.charAt(i);
// 如果是数字字符0-9
if (Character.isDigit(digit)) { if (Character.isDigit(digit)) {
int value = Character.getNumericValue(digit); int value = Character.getNumericValue(digit);
decimal += value * Math.pow(base, power); decimal += value * Math.pow(base, power);
} }
// 如果是字母字符A-F 或 a-f表示10-15
else if (Character.isLetter(digit)) { else if (Character.isLetter(digit)) {
char upperDigit = Character.toUpperCase(digit); char upperDigit = Character.toUpperCase(digit);
int value = upperDigit - 'A' + 10; int value = upperDigit - 'A' + 10;
@ -31,14 +29,14 @@ public class uml10_10 {
return decimal; return decimal;
} }
// 将十进制整数转换为任意进制字符串 // 将十进制整数转换为任意进制字符串
public static String fromDecimal(int number, int base) { public static String fromDecimal(int number, int base) {
if (number == 0) { if (number == 0) {
return "0"; return "0";
} }
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制 char[] digits = "0123456789ABCDEF".toCharArray(); // 支持到16进制
boolean isNegative = number < 0; boolean isNegative = number < 0;
if (isNegative) { if (isNegative) {
@ -59,34 +57,67 @@ public class uml10_10 {
} }
public static void main(String[] args) { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 创建窗口
JFrame frame = new JFrame("进制转换器");
System.out.print("请输入源进制2-16"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int sourceBase = scanner.nextInt(); frame.setSize(400, 300);
scanner.nextLine(); // 消耗掉换行符 frame.setLayout(new GridLayout(5, 2)); // 网格布局
System.out.print("请输入目标进制2-16"); // 创建组件
int targetBase = scanner.nextInt(); JLabel sourceBaseLabel = new JLabel("源进制2-16");
scanner.nextLine(); // 消耗掉换行符 JTextField sourceBaseField = new JTextField();
System.out.print("请输入要转换的" + sourceBase + "进制数字:"); JLabel targetBaseLabel = new JLabel("目标进制2-16");
String number = scanner.nextLine().trim(); // 读取实际的数字输入 JTextField targetBaseField = new JTextField();
// 验证源进制数字是否有效 JLabel numberLabel = new JLabel("要转换的数字:");
for (char c : number.toCharArray()) { JTextField numberField = new JTextField();
if (!Character.isDigit(c) && !(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
throw new IllegalArgumentException("Invalid character in source base number: " + c); JButton convertButton = new JButton("转换");
JLabel resultLabel = new JLabel("转换结果:");
// 添加组件到窗口
frame.add(sourceBaseLabel);
frame.add(sourceBaseField);
frame.add(targetBaseLabel);
frame.add(targetBaseField);
frame.add(numberLabel);
frame.add(numberField);
frame.add(convertButton);
frame.add(resultLabel);
// 添加按钮事件处理
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int sourceBase = Integer.parseInt(sourceBaseField.getText().trim());
int targetBase = Integer.parseInt(targetBaseField.getText().trim());
String number = numberField.getText().trim();
// 验证源进制数字是否有效
for (char c : number.toCharArray()) {
if (!Character.isDigit(c) &&
!(Character.isLetter(c) && (Character.toUpperCase(c) >= 'A' && Character.toUpperCase(c) <= 'F') && sourceBase > 10)) {
throw new IllegalArgumentException("Invalid character in source base number: " + c);
}
}
// 将源进制数字转换为十进制
int decimalNumber = toDecimal(number, sourceBase);
// 将十进制数字转换为目标进制
String convertedNumber = fromDecimal(decimalNumber, targetBase);
// 显示结果
resultLabel.setText("转换后的" + targetBase + "进制数字是:" + convertedNumber);
} catch (NumberFormatException | IllegalArgumentException ex) {
JOptionPane.showMessageDialog(frame, "错误: " + ex.getMessage(), "输入错误", JOptionPane.ERROR_MESSAGE);
}
} }
} });
// 将源进制数字转换为十进制
int decimalNumber = toDecimal(number, sourceBase);
// 将十进制数字转换为目标进制
String convertedNumber = fromDecimal(decimalNumber, targetBase);
System.out.println("转换后的" + targetBase + "进制数字是:" + convertedNumber);
scanner.close(); // 显示窗口
frame.setVisible(true);
} }
} }

Loading…
Cancel
Save