Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 7 years ago

Struggling with Java programming book problem. Help?

Here's the problem from the book:

Write a method called printSquare that accepts a minimum and maximum integer and prints a square of lines of increasing numbers like the following:

printSquare(3, 7);

should produce the following:

34567

45673

56734

67345

73456

This is what I have so far:

public class book

{

public static void main(String[]args)

{

printSquare(3, 7);

}

public static void printSquare(int n, int x)

{

//for (int a = n; a <= x; a++) not sure what to put here

{

for (int i = n; i <= x; i++)

{

System.out.print(i);

}

System.out.println();

}

}

}

1 Answer

Relevance
  • 7 years ago
    Favorite Answer

    Here is my soulution:

    public static void printSquare(int a, int b)

    {

    for (int inc = 0; inc < b - a + 1; inc++)

    {

    for (int i = a + inc; i < b + 1; i++)

    {

    System.out.print(i);

    }

    for (int j = 0; j < inc; j++)

    {

    System.out.print(a + j);

    }

    System.out.println();

    }

    }

    Source(s): Me, my brain
Still have questions? Get your answers by asking now.