jinzhi_tanchuang

main
guixu0320 1 month ago
parent 2ccfceb2a6
commit 7158056fff

@ -1,8 +1,8 @@
import java.util.Scanner; import javax.swing.JOptionPane;
public class jinzhi { public class jinzhi {
// 将数字从 'fromBase' 转换为 'toBase' // 将数字从 'fromBase' 转换为 'toBase'111
public static String convertBase(String number, int fromBase, int toBase) { public static String convertBase(String number, int fromBase, int toBase) {
// 第一步:从 'fromBase' 转换为十进制base 10 // 第一步:从 'fromBase' 转换为十进制base 10
int decimalNumber = 0; int decimalNumber = 0;
@ -43,28 +43,24 @@ public class jinzhi {
} }
public static void main(String[] args) { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入数字和基数 // 输入数字和基数
System.out.print("请输入数字: "); String number = JOptionPane.showInputDialog("请输入数字:");
String number = scanner.nextLine(); String fromBaseInput = JOptionPane.showInputDialog("请输入原始进制 (2 到 16):");
int fromBase = Integer.parseInt(fromBaseInput);
System.out.print("请输入原始进制 (2 到 16): "); String toBaseInput = JOptionPane.showInputDialog("请输入目标进制 (2 到 16):");
int fromBase = scanner.nextInt(); int toBase = Integer.parseInt(toBaseInput);
System.out.print("请输入目标进制 (2 到 16): ");
int toBase = scanner.nextInt();
// 验证基数 // 验证基数
if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) { if (fromBase < 2 || fromBase > 16 || toBase < 2 || toBase > 16) {
System.out.println("基数必须在 2 到 16 之间。"); JOptionPane.showMessageDialog(null, "基数必须在 2 到 16 之间。");
return; return;
} }
// 转换数字并打印结果 // 转换数字并打印结果
String result = convertBase(number, fromBase, toBase); String result = convertBase(number, fromBase, toBase);
System.out.println("进制为 " + fromBase + " 的数: " + number + " 在进制为 " + toBase + " 的数是: " + result ); String message111 = "进制为 " + fromBase + " 的数: " + number + " 在进制为 " + toBase + " 的数是: " + result;
scanner.close(); // 使用弹窗显示结果
JOptionPane.showMessageDialog(null, message111);
} }
} }

Loading…
Cancel
Save