Display patterns on the screen
I am a beginner in java programming and I need help to use nested loop to print out the following pattern on the screen. The first number in each pair represents the row number and the second one represents the column number.
The sample output is:
1,1 1,2 1,3 1,4
2,1 2,2 2,3 2,4
3,1 3,2 3,3 3,4
Here is what I have done so far:
/*Use nested loop to print out the following pattern on the screen*/
class Pattern {
public static void main(String[] args){
int row =3;
int col =4;
for(i =0; i<row; i++)
{
for(intj=0; j<col; j++)
{
System.out.println(1,1 );
}
}
}
Re: Display patterns on the screen
Have you determined the rules for generating the contents of each line?
Can you explain what they are?
For example columun one appears to be the line number, starting at 1.
What goes in the rest of the columns on the line?
Re: Display patterns on the screen
Thanks for the response.
Here are the responses to your questions:
The row numbers stay constant but the column number increases
2, 3, 4 go in the rest of the columns on the line.
Thanks.
Re: Display patterns on the screen
If you have the rules for generating the numbers that go on each line, try writing the program that will produce those numbers.
I don't see that your explanation shows how to generate numbers such that the second column is all 1s and the fourth column is all 2s.
Re: Display patterns on the screen
Here is the assignment question:
Use nested loop to print out the following pattern on the screen: the first number in each pair represents the row number and the second one represents the column number. You need to declare and use tow "int" variables: the total number of rows and the total number of columns. So the actual input will change accordingly if the numebr of rows/columns is changed. For example, the following example is printed when the number of rows is set to 3 and the number of columns is set to 4.
Output: 1,1 1,2 1,3 1,4
2,1 2,2 2,3 2,4
3,1 3,2 3,3 3,4
The Other Questions is:
Based on question above, this time only display the number pair when the row number and the column number are the same, otherwise display * . Use both "for" and "while" loop to achieve the same results.
Thanks for your help!
Re: Display patterns on the screen
Have you worked out what the contents of each row is supposed to be and how to generate them?
This sentence tells you:
Quote:
the first number in each pair represents the row number
and the second one represents the column number.
You will need to use some loops to print out the numbers. You need to consider when to output the ',' and when the ' '.
Re: Display patterns on the screen
Could you please help me with the solution since I need to submit it tonight. Thanks in advance.
Re: Display patterns on the screen
Start with printing out one row of output row,col number pairs built from these: row number, a comma, column number and a space
Use a loop to change the column number value from 1 to 4.
print it on one line by using the print() method for each r,c pair.
After all the data for one line has been printed in the loop, use the println() method to move to the next line.
Re: Display patterns on the screen
Does that println statement compile?
Re: Display patterns on the screen
Quote:
Originally Posted by
felimblack
You should to use println(i, j) instead (again, I cut some text).
Where in the API for PrintStream is the method definition for println(int, int) ?
PrintStream (Java Platform SE 6)
Hint: it doesn't exist.