列入有000000111
转换成
000
000
111
我只写到了将任意十进制转换成二进制- -
求大神指点指点
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Conversation {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("请输入二进制数字: ");
int m=input.nextInt();
toBinary(m);
}
static void toBinary(int num){
if(num/2==0){
int w = num%2;
System.out.print(w+" ");
}
else{
int w = num%2;
toBinary(num/2);
System.out.print(w+" ");
}
}
}
额,这是把二进制转换成十进制的....
package com.rui.test; public class test { public static void main(String[] args) { //定义一个二进制的数 String str="1010"; //将这个二进制的字符串进行反串 char [] ch=str.toCharArray(); for (int i = 0; i < ch.length; i++) { char c=ch[i]; ch[i]=ch[ch.length-i- 1]; ch[ch.length-i-1]=c; if ((ch.length-1)/2 ==i) { break; } } StringBuffer strbuff=new StringBuffer(String.copyValueOf(ch)); int num=0; for (int i = 0; i < strbuff.length(); i++) { num+=(strbuff.charAt(i)-48)*Math.pow(2,i); } System.out.println(num); } }