美文网首页
Print n length binary number

Print n length binary number

作者: wxqyppqm | 来源:发表于2016-12-04 20:00 被阅读0次
    
    import java.util.Scanner;
    
    class printBinary 
    {
      void printB (int n)
      {
        for (int i=0; i< Math.pow(2,n); i++)  // 1<<n
        {
            StringBuffer sb = new StringBuffer();
            int temp = i;
            
            for(int j=0; j<n; j++){
            sb.insert (0, temp%2);
            //sb = ((temp%2) == 1) ? sb.insert(0,"1") : sb.insert(0,"0");
            temp = temp/2;
            }
            System.out.println(sb);
        }
      }
    }
    
    public class Runner
    {
       public static void main(String[] args)
       {
          Scanner scan = new Scanner (System.in);
          System.out.println("Enter n:");
          int n = scan.nextInt();
          
          printBinary pB = new printBinary();
          pB.printB(n);
       }
    }   
    
    

    相关文章

      网友评论

          本文标题:Print n length binary number

          本文链接:https://www.haomeiwen.com/subject/denhmttx.html