diff --git a/.idea/misc.xml b/.idea/misc.xml index e1f830b..0548357 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 5aebfd1..8c68c6b 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ - - + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 93490c0..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# pan - diff --git a/chen123/src/Main.java b/chen123/src/Main.java deleted file mode 100644 index 3e59c38..0000000 --- a/chen123/src/Main.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/chen123/src/jisuan.java b/chen123/src/jisuan.java deleted file mode 100644 index 1107087..0000000 --- a/chen123/src/jisuan.java +++ /dev/null @@ -1,211 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -public class jisuan{ - public static void main(String[] args) { - JS win=new JS(); - } -} -class JS extends JFrame implements ActionListener{ - private StringBuilder sBuilder = new StringBuilder(); - JTextArea text=new JTextArea(); - double a,b; - Double sum; - int i; - public JS() - { - setBounds(100,100,400,400); - setTitle("计算器"); - this.setLayout(new BorderLayout()); - JPanel p1=new JPanel(); - JPanel p2=new JPanel(); - text.setPreferredSize(new Dimension (370,60)); - p2.setLayout(new FlowLayout()); - p1.add(text); - this.add(p1,BorderLayout.NORTH); - - - p2.setLayout(new GridLayout(5,4)); - JButton button[]=new JButton[20]; - button[0]=new JButton("C"); - button[1]=new JButton(" "); - button[2]=new JButton("%"); - button[3]=new JButton("÷"); - button[4]=new JButton("7"); - button[5]=new JButton("8"); - button[6]=new JButton("9"); - button[7]=new JButton("x"); - button[8]=new JButton("4"); - button[9]=new JButton("5"); - button[10]=new JButton("6"); - button[11]=new JButton("—"); - button[12]=new JButton("1"); - button[13]=new JButton("2"); - button[14]=new JButton("3"); - button[15]=new JButton("+"); - button[16]=new JButton(""); - button[17]=new JButton("0"); - button[18]=new JButton("."); - button[19]=new JButton("="); - - for(int i=0;i - + \ No newline at end of file diff --git a/jinzhi/src/JinZhi.java b/jinzhi/src/JinZhi.java new file mode 100644 index 0000000..6b26597 --- /dev/null +++ b/jinzhi/src/JinZhi.java @@ -0,0 +1,103 @@ +import java.util.Scanner; + +public class JinZhi { + + // 辅助方法:将任意进制的数转换为十进制 + /** + * 将任意进制的数转换为十进制数 + * + * @param number 要转换的数的字符串表示 + * @param base 该数的原始进制 + * @return 转换后的十进制数 + */ + public static int toDecimal(String number, int base) { + int decimalValue = 0; // 十进制结果 + int power = 0; // 当前位的权重(幂次) + + // 从字符串的最后一个字符开始遍历 + for (int i = number.length() - 1; i >= 0; i--) { + char digit = number.charAt(i); // 当前字符 + int value = 0; // 当前字符对应的十进制值 + + // 根据字符计算其十进制值 + if (Character.isDigit(digit)) { + value = digit - '0'; // 如果是数字,直接减去'0'的ASCII码 + } else if (digit >= 'A' && digit <= 'F') { + value = digit - 'A' + 10; // 如果是大写字母,减去'A'的ASCII码后加10 + } else if (digit >= 'a' && digit <= 'f') { + value = digit - 'a' + 10; // 如果是小写字母,减去'a'的ASCII码后加10 + } else { + throw new IllegalArgumentException("输入的数字中包含无效字符: " + digit); + } + + // 将当前字符的十进制值乘以权重后累加到结果中 + decimalValue += value * Math.pow(base, power); + power++; // 权重增加 + } + + return decimalValue; + } + + // 辅助方法:将十进制数转换为任意进制 + /** + * 将十进制数转换为任意进制的数 + * + * @param decimalValue 要转换的十进制数 + * @param base 目标进制 + * @return 转换后的目标进制数的字符串表示 + */ + public static String fromDecimal(int decimalValue, int base) { + if (decimalValue == 0) { + return "0"; // 如果十进制数为0,直接返回"0" + } + + StringBuilder result = new StringBuilder(); // 用于存储转换结果的StringBuilder对象 + char[] digits = "0123456789ABCDEF".toCharArray(); // 表示所有可能的进制字符的数组 + + // 使用循环进行进制转换 + while (decimalValue > 0) { + int remainder = decimalValue % base; // 取余数作为当前位的值 + result.insert(0, digits[remainder]); // 将当前位的值添加到结果的开头 + decimalValue /= base; // 十进制数除以目标进制,为下一次循环做准备 + } + + return result.toString(); // 返回转换后的结果字符串 + } + + // 主方法:执行进制转换 + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); // 创建Scanner对象用于读取用户输入 + + // 读取要转换的数、其原始进制和目标进制 + System.out.print("请输入要转换的数: "); + String number = scanner.nextLine(); + System.out.print("请输入该数的原始进制(2-16): "); + int inputBase = scanner.nextInt(); + + // 读取目标进制 + System.out.print("请输入目标进制(2-16): "); + int targetBase = scanner.nextInt(); + + // 验证输入的有效性 + if (inputBase < 2 || inputBase > 16 || targetBase < 2 || targetBase > 16) { + System.out.println("无效的进制。进制必须在2到16之间。"); + return; // 如果输入无效,则直接返回,不执行后续操作 + } + + try { + // 将输入的数转换为十进制数 + int decimalValue = toDecimal(number, inputBase); + + // 将十进制数转换为目标进制数 + String result = fromDecimal(decimalValue, targetBase); + + // 输出转换后的结果 + System.out.println(number + "初始是" + inputBase + "进制;" + "在" + targetBase + "进制下是"+ result); + } catch (IllegalArgumentException e) { + // 捕获并处理非法参数异常 + System.out.println("错误: " + e.getMessage()); + } + + scanner.close(); // 关闭Scanner对象,释放资源 + } +} diff --git a/out/production/chen123/JS.class b/out/production/chen123/JS.class deleted file mode 100644 index abd5f7d..0000000 Binary files a/out/production/chen123/JS.class and /dev/null differ diff --git a/out/production/chen123/Main.class b/out/production/chen123/Main.class deleted file mode 100644 index baa3095..0000000 Binary files a/out/production/chen123/Main.class and /dev/null differ diff --git a/out/production/chen123/jisuan.class b/out/production/chen123/jisuan.class deleted file mode 100644 index 4153de1..0000000 Binary files a/out/production/chen123/jisuan.class and /dev/null differ diff --git a/out/production/jinzhi/JinZhi.class b/out/production/jinzhi/JinZhi.class new file mode 100644 index 0000000..c328a5f Binary files /dev/null and b/out/production/jinzhi/JinZhi.class differ diff --git a/.idea/pan.iml b/test6298.iml similarity index 58% rename from .idea/pan.iml rename to test6298.iml index d6ebd48..8b2ade9 100644 --- a/.idea/pan.iml +++ b/test6298.iml @@ -1,8 +1,10 @@ - + - + + +