package test;
//辗转相除法来做
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* 十进制转化为二进制
*/
public class DtoB {
public static void main(String[] args){
ArrayList<Integer> lists=new ArrayList<Integer>();
System.out.println("请输入需要处理的数字");
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
try {
int num=Integer.parseInt(bf.readLine());
if(num==0||num==1){
lists.add(num);
}else{
while(num>=2){
int extra=num%2;
lists.add(extra);
num=num/2;
if(num<2){
lists.add(num);
}
}
}
System.out.print("二进制转化结果:");
for (int i=lists.size()-1;i>=0;i--){
System.out.print(lists.get(i));
}
}catch (Exception e){
e.printStackTrace();
}
}
} |
|