import java.util.*;
class DecBinSwitch 
{
    DecBinSwitch()
    {}
    public void DecSwitchBin()
    {
    	int bin;
    	int flag=0;
        int array[]=new int[8];
    	Scanner sc=new Scanner(System.in);
    	System.out.println("please input integer number :");
    	bin=sc.nextInt();
    	if(bin==0)
    	{
    		for(int i=0;i<array.length;i++)
    		{
    			array[i]=0;
    			System.out.print(array[i]+" ");
    		}
    	}
    	else if(bin<0)
    	{
    		flag=1;
    		bin*=(-1);
    	}
    	array[array.length-1]=flag;
    	for(int i=0;i<array.length-1;i++)
    	{
    		if(bin!=0)
    		{
    			array[i]=bin%2;
    			bin/=2;
    		}
    	}
    	if(flag==1)
    	{
    		int i=0;
    		for(i=0;i<array.length-1&&array[i]!=1;i++);
    		i++;
    		while(i!=array.length-1)
    		{
    			if(array[i]==1)
    			{
    				array[i]=0;
    			}
    			else
    			{
    				array[i]=1;
    			}
    			i++;
    		}
    	}
    	for(int i=array.length-1;i>=0;i--)
    	{
    		System.out.print(array[i]+" ");
    	}
    }
    public void BinSwitchDec()
    {
    	int [] array=new int[8];
    	int sum=0;
    	Scanner sc1=new Scanner(System.in);
    	System.out.println("please input eight Binarynumber :");
    	for(int i=0;i<array.length;i++)
    	{
    		array[i]=sc1.nextInt();
    	}
    	if(array[0]==1)
    	{
    		int i=0;
    		for(i=array.length-1;i>0&&array[i]!=1;i--);
    		if(i==0)
    		{
    			System.out.println("-1");
    		}
    		else
    	 { 
    		i--;
    		while(i!=0)
    		{
    			if(array[i]==1)
    			{
    				array[i]=0;
    			}
    			else if(array[i]==0)
    			{
    				array[i]=1;
    			}
    			i--;
    		}
    		for(int j=array.length-1;j>0;j--)
    		{
    			sum+=array[i]*Math.pow(2.0, array.length-1-j);
    		}
    		System.out.println(sum*(-1));
    	}
    		
    	}
    	else
    	{
    		for(int k=array.length-1;k>0;k--)
    		{
    			sum+=array[k]*Math.pow(2.0, array.length-1-k);
    		}
    		System.out.println(sum);
    	}
    }
}
最近下载更多
樊樊樊樊樊樊阵雨  LV18 2019年8月12日
FFFFFly  LV3 2019年7月9日
2048凯莱  LV9 2019年3月11日
彭鑫233  LV1 2018年11月10日
gaoxin222  LV14 2018年10月12日
dingjjd  LV1 2018年3月10日
小小纠结伦  LV17 2015年8月3日
恨不得  LV9 2015年5月21日
最近浏览更多
tyw555 2023年11月14日
暂无贡献等级
crosa_Don  LV18 2022年7月6日
1811841085  LV3 2021年3月18日
w19991213  LV2 2020年9月21日
hyuxsda  LV1 2019年12月27日
樊樊樊樊樊樊阵雨  LV18 2019年8月12日
FFFFFly  LV3 2019年7月9日
2048凯莱  LV9 2019年3月11日
756398953  LV12 2019年2月19日
68Since 2018年12月19日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友