動手實現二進位制轉換十進位制
阿新 • • 發佈:2018-12-21
public class MathDemo { public static void main(String[] args) { // 二進位制轉換十進位制 1100 String num = "1100"; // 拆分成字元陣列 char[] ch = num.toCharArray(); int total = 0; int[] number = new int[num.length()]; int j = num.length() - 1; int totalNum=diGui(ch,total,number,j); System.out.println(totalNum); } public static int diGui(char[] ch, int total, int[] number, int j) { if (j >= 0) { for (int i = 0; i < ch.length; i++) { String s = ch[i] + ""; number[i] = Integer.parseInt(s);// 1,1,0,0 int temp = ((int) Math.pow(2, j));// 3,2,1,0 8 total += number[i] * temp;// 1*8+ //System.out.println(total); j--; diGui(ch, total, number, j); } } return total; } }
新手一枚,寫的不好的地方還望指正。