本帖最后由 王舜民 于 2012-8-28 08:43 编辑
//首先你这个程序没写全吧
public class IntToBinaryDemo
{
public static void main(String[] args)
{
int num =6;
toBinary2(num);
}
public static void toBinary2(int num)
{
if(num>0)
{
toBinary2(num/2);//6>0 调用toBinary2(num/2);
//3>0满足调用toBinary2,
//num变为1.
//实际
//打印是从1开始的 :1%2=1;3%2=1;6%2=0 =>110
System.out.print(num%2);
}
}
}
第二种 ///////////////////////////
public class IntToBinaryDemo
{
public static void main(String[] args)
{
int num =6;
toBinary2(num);
}
public static void toBinary2(int num)
{
if(num>0)
{
System.out.print(num%2);
//[1]6>0;打印6%2=0;此时
//[2]6执行递归操作 toBinary2 6=》3 ;3》0
// 打印 3%2=1
//[3]3执行递归操作 3=》1;
// 1满足》0
// 打印 1%2=1
// 即为:011
toBinary2(num/2);
}
}
}
|