For loop excercise


I recently started on my new education here in Denmark which focuses on system dev and programming. I'm going through some of the initial programming excercises and were doing for loops atm.

We are using the book "Building Java Programs - A back to basics approach" and im doing an excercise that goes like this:

[b]4. Write a method called printSquare that accepts a minimum and maximum integer and prints a square of lines of
increasing numbers. The first line should start with the minimum, and each line that follows should start with the
next-higher number. The sequence of numbers on a line wraps back to the minimum after it hits the maximum. For
example, the call printSquare(3, 7);

should produce the following output:
If the maximum passed is less than the minimum, the method produces no output.[/b]

Currently my code is looking like this, but I can't seem to get the last part right:

[code]public class Opg4Kap3 {

public static void main(String[] args) {

printSquare(3, 7);


public static void printSquare(int a, int b) {

for (int k=a ; k<=b ; k++) {

for (int i=k+1 ; i<=b ; i++) {


for (int r=a ; r<=6 ; r++) {




We don't need to turn this in or anything so I'm not really interested in the final answer, was just hoping someone could give me a clue to the next step so I can finish it though, since it annoys me I can't get the last part right (or if I have to do something completely different to begin with?).

Also this is not supposed to be done with anything but a method and for loops so no if statement tips or anything.

Thanks in advance
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!