当前位置: 代码迷 >> Java相关 >> 二进制转十进制使用FOR循环来实现------------------>大家请进
  详细解决方案

二进制转十进制使用FOR循环来实现------------------>大家请进

热度:149   发布时间:2009-09-01 18:37:37.0
二进制转十进制使用FOR循环来实现------------------>大家请进
我的思路是这样的。我要怎么样才能达到这个转换的目的呢?具体怎么去写代码呢?
public class ToBin {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.println("欢迎进入二进制转十进制系统\n请输入一个数:");
        int num=input.nextInt();
        int x;
        int[] ten=new int[100];
        int[] two=new int[100];
        for(x=0;x<100;x++){
            ten[x]=num%10;
            num=num/10;
            if(num<1){
                break;
            }
            x+=(int)(Math.pow(2, ten[x]));
        }
      
        System.out.println(x);
  }
}

[ 本帖最后由 gameohyes 于 2009-9-1 19:47 编辑 ]
搜索更多相关的解决方案: 二进制  FOR  十进制  

----------------解决方案--------------------------------------------------------
可以直接用Integer.parseInt("1010010",2);来实现
----------------解决方案--------------------------------------------------------
程序代码:
这是它的源码
    public static int parseInt(String s, int radix)
  throws NumberFormatException
    {
        if (s == null) {
            throw new NumberFormatException("null");
        }
if (radix < Character.MIN_RADIX) {
     throw new NumberFormatException("radix " + radix +
         " less than Character.MIN_RADIX");
}
if (radix > Character.MAX_RADIX) {
     throw new NumberFormatException("radix " + radix +
         " greater than Character.MAX_RADIX");
}
int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;
if (max > 0) {
     if (s.charAt(0) == '-') {
  negative = true;
  limit = Integer.MIN_VALUE;
  i++;
     } else {
  limit = -Integer.MAX_VALUE;
     }
     multmin = limit / radix;
     if (i < max) {
  digit = Character.digit(s.charAt(i++),radix);
  if (digit < 0) {
      throw NumberFormatException.forInputString(s);
  } else {
      result = -digit;
  }
     }
     while (i < max) {
  // Accumulating negatively avoids surprises near MAX_VALUE
  digit = Character.digit(s.charAt(i++),radix);
  if (digit < 0) {
      throw NumberFormatException.forInputString(s);
  }
  if (result < multmin) {
      throw NumberFormatException.forInputString(s);
  }
  result *= radix;
  if (result < limit + digit) {
      throw NumberFormatException.forInputString(s);
  }
  result -= digit;
     }
} else {
     throw NumberFormatException.forInputString(s);
}
if (negative) {
     if (i > 1) {
  return result;
     } else { /* Only got "-" */
  throw NumberFormatException.forInputString(s);
     }
} else {
     return -result;
}
    }bccn_1251807533254976217543706

----------------解决方案--------------------------------------------------------
如果这样子写呢?  该怎么去修改:
    public static void main(String[] args){
        String[] num1=new String[100];
        int x,y,z=0;
        Scanner input=new Scanner(System.in);
        System.out.println("欢迎进入二进制转十进制系统\n请输入一个数:");
        int num=input.nextInt();
         int[] ten=new int[100];
        int[] two=new int[100];
        for(x=0;x<100;x++){
            ten[x]=num%10;
            num=num/10;
            if(num<1){
                break;
            }
            
        }
        num1[x]=String.valueOf(ten[x]);
        for(y=x;y>=0;y++){
            z+=(int)(Math.pow(2, num1.length()-1));
            System.out.println(z);
        }
----------------解决方案--------------------------------------------------------
  相关解决方案