《【2017年整理】JAVA二进制转换为十进制》由会员分享,可在线阅读,更多相关《【2017年整理】JAVA二进制转换为十进制(2页珍藏版)》请在金锄头文库上搜索。
1、import java.util.Scanner;public class binaryToDecimal /* param args*/public static int binary(String str)/Scanner input=new Scanner(System.in);/System.out.print(Please input your string:);/String str=input.next();int len=str.length();char ch;int num=0;for(int i=len-1;i=0;i-)ch=str.charAt(i);if(ch!=0
2、&ch!=1)System.out.println(input wrong!);break;if(ch=1)num= (int) (num+Math.pow(2,(len-1)-i);return num;public static void main(String args) / TODO Auto-generated method stubScanner input=new Scanner(System.in);while(true)System.out.print(Please input your string:);String str=input.next();int total;total=binary(str);System.out.println(total=+total);