I need assistance with the following:

Write the Java code which uses iteration to produce the following output::
1
1 2
1 2 3
1 2 3 4
////////////////////////////////////////////////////////////////

My Source code: (Not correct) Please review, I must include a loop to produce output as follows:

1
1 2
1 2 3
1 2 3 4

My code is posted below, I have a working copy but is not 100% accurate. This is due in a matter of hours and I really need assistance to ensure that I get this one right.

///////////////////////////////////////////////////////////

import java.util.*;

public class OneTwoThreeFourIteration
{
public static void main(String[] args)
{
int count, number;
System.out.println("Enter number 4");
Scanner keyboard = new Scanner(System.in);
number = keyboard.nextInt( );
count = 1;
do
{
System.out.print(count + ", ");
count++;
}while (count <= number);
System.out.println( );
System.out.println("Here is the requested results.");
System.out.println("1");
System.out.println("1 2");
System.out.println("1 2 3");
System.out.println("1 2 3 4");
System.out.println("The End!");
}
}

## All 3 Replies

To get output like...
1
1 2
1 2 3
1 2 3 4
There are 2 ways (that come to mind) to do it, not sure which way would be easier. But i'm betting that you are in the part of your class in which you are covering or have covered nested loops. Which is why i chose that for the example. Anytime you have a matrix or table you will want to use nested loops (there are exceptions but thats a decent rule). The first loop handles the row, the second obviously handles the column. These can be reversed but i dont really recommend it. The following code works and is definately not the best code in the world, but since so many people on here are just worried about getting something that works, which is always 100% better then code that doesn't work, i didn't think you would care much. Enjoy.

``````public class OneTwoThreeFourIteration
{
public static void main(String[] args)
{
int number;
System.out.println("Enter last number: ");
Scanner keyboard = new Scanner(System.in);
number = keyboard.nextInt();
for (int i = 1; i <= number; i++)
{
String line = new String();
for (int j = 1; j <= i; j++)
{
line = line + j + " ";
}
System.out.println(line);
}
}
}``````

I am learning more about programming by posting questions here than I ever imagined, Thanks once again for the assistance. Taking this class via DE was not the best choice. Hopefully I will gain a better understanding and be able to assist others as you have helped me.

Get a good book and use that.
Good books are:
Head First Java (2nd edition) by Kathy Sierra and Bert Bates
Agile Java by Jeff Langr

Loops are so basic you should pick them up easily from any tutorial, and not need someone else to write them out for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.