0

This is a simple program i wrote using for loop

public class Test
{
    public static void main (String[] args)
    {

        int high=10;

        for( int low =0; low <=high; low++){

            for (int mid=0; mid<=high; mid++)
            {
                System.out.print(mid);

            }
            System.out.println();    
        }
    }
}

But I want the output to look like

0 1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

2 3 4 5 6 7 8 9 10

etc...

10

Instead my output looks like this 012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910

What am i doing wrong?

1
  • Set mid=low in the second loop and you are also missing the opening { for the same loop. Commented Mar 25, 2011 at 4:12

8 Answers 8

4

You're not printing out any spaces.

System.out.print(mid + " ");

edit:

In addition, you're starting mid at 0 every time through the inner loop rather than starting it at low.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats not the answer he wants... His numbers keep coming out like 0 1 2 3 4 5 6 7 8 9 10 each time. He wants it to start one number higher each iteration. He has a logic error I just havent been able to work it out.
This is what i got after the mid+" " 0 1 2 3 4 ect
Well, it's one of the problems he's getting. Should be fixed now
2
System.out.print(mid + " ");

and

System.out.println(" ");

Should fix you up.

EDIT: Oh ... well, that and:

for (int mid = low; mid<=high; mid++)

1 Comment

From the last System.out.println, you can omit the empty string like he did in his code. It will do essentially the same thing.
1

You may want to concatenate a space to your System.out call.

System.out.print( mid + " " );

Comments

1

Set mid = low in the inner loop.

Comments

1

You want to set mid = low, and print spaces as others have noted:

int high=10;

for( int low =0; low <=high; low++)
    for (int mid=low; mid<=high; mid++)
    {
        System.out.print("%d ", mid);

    }
    System.out.println();
 }

Edit: removed the spurious \n.

Comments

1

Following code will do

int high = 10;
    for (int low = 1; low <= high; low++) {
        for (int mid = low; mid <= high; mid++) {
            System.out.print(mid + " ");
        }
        System.out.println();
    }
}

Comments

1

I hope this gets you what you want :

public class Test
{
    public static void main (String[] args)
    {

    int high=10;

    for( int low =0; low <=high; low++)
    {
        for (int mid=low; mid<=high; mid++) //start from low not 0
        {
            System.out.print(mid+" ");

        }
        System.out.println();

    }
    }
}

1 Comment

Thanks for the help, i guess i forgot to have a do loop nested under the first do loop
0

Try this

public class Test {
    public static void main (String[] args) {

    int high=10;

    for( int low =0; low<=high; low++) {

        for (int mid=low; mid<=high; mid++) {
            System.out.print(mid + " " );

        }

        System.out.println();
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.