/**
* 十进制转换二进制,而且是正序排列
*/
public class Demo9 {
public static void toBin(int num)
{
if (num > 0)
{
StringBuffer sb = new StringBuffer();
while (num > 0)
{
sb.append(num % 2);
num = num / 2;
}
System.out.println(sb.reverse());
if (1 > 0)
{
xianShi();
}
}
else
{
num=-num;
StringBuffer sb = new StringBuffer();
while (num > 0)
{
sb.append(num % 2);
num = num / 2;
}
System.out.println(sb.reverse());
}
}
public static void xianShi()
{
Scanner s = new Scanner(System.in);
System.out.println("十进制数");
int x = s.nextInt();
toBin(x);
}
public static void main(String[] args)
{
xianShi();
}
}
|
|