-1
7
14 15
28 29 30 31
56 57 58 59 60 61 62 63

This is the way in which I want to print but couldn't. The code I have written is like:

public class Numbers
{
    public void printseries(int a, int b)
    {
        for (int i = 1; i <= 2 * b; )
        {
            int k = 0;
            k = a * i;
            i *= 2;
            for (int j = 1; j < i; j *= 2)
            {
                if (j == 1)
                    System.out.print(k);
                else
                {   
                    k++;
                    System.out.print(k);
                }
            }
        }
    }
}

It prints the following

7141528293056575859
4

3 回答 3

3

而不是使用print()useprintln()来代替。这将导致控制台在每次调用该方法时打印出一个新行。"\n"如果您愿意,也可以在打印方法中使用转义字符。

编辑这可能是您正在寻找的代码

class Numbers 
{
public void printseries(int a, int b)
{
    for(int i=1;i<=2*b;)
    {
        int k=0;
        k=a*i;
        i*=2;
        for(int j=1;j<i;j*=2)
        {
            if(j==1)
            System.out.println(k);
            else
            {   
                k++;
            System.out.print(k + " ");
            }       
        }
    }
}
}
于 2013-07-07T04:01:04.037 回答
0

尝试这个

    void printSeries(int a, int b)
    {
      int otherNumbs=1;

     for(int i=1;i<2*b;i*=2)
     {
       int k = i*a;
       int temp = k;

       for(int j = 0;j<otherNumbs;++j)
       {
         System.out.print(k++ + " ");
       }

       k = temp ;
       otherNumbs *= 2;
       System.out.println();
     }
    }

如果这有帮助。乐趣。

于 2013-07-07T05:07:17.323 回答
0

尝试这个

public void printseries(int a, int b)
    {
        for (int i = 1; i <= 2 * b; )
        {
            int k = 0;
            k = a * i;
            i *= 2;
            for (int j = 1; j <= i/2; j++)
            {
                if (j == 1)
                    System.out.print(k);
                else
                {   
                    k++;
                    System.out.print(" "+k);
                }
            }
            System.out.println();
        }
    }

或者

你也可以试试这个

public void printseries(int a, int b)
    {
        for (int i = 1; i <= 2 * b; i*=2)
        {
            int k = 0;
            k = a * i;
            for (int j = 1; j <= i; j++)
            {
                if (j == 1)
                    System.out.print(k);
                else
                {   
                    k++;
                    System.out.print(" "+k);
                }
            }
            System.out.println();
        }
    }
于 2013-07-07T04:18:14.860 回答