문제

The pattern should look like this :

12345
22345
33345
44445
55555

I have already tried:

 import java.io.*;
 class Pattern
 {
    void display() 
    {
        int i=0,k;
        for(i=1;i<=5;i++)
        {
             for(k=1;k<=5;k++)
             {
                 System.out.print(i+" ");
             }
             System.out.println();
        }
    }    
}
도움이 되었습니까?

해결책

Try this:

void display() 
{
    int i=0,k;
    for(i=1;i<=5;i++)
    {
         for(k=1;k<=5;k++)
         {
             if (k < i ) {
                 System.out.print(i);
             } else {
                 System.out.print(k);
             }
         }
         System.out.println();
    }
}

Your pattern is dependant on the line number i:

  1. Line n should print n, n times
  2. fill the remainder of the line with incresing integers starting with n up to the length of the line/max number of lines.

You could generalize it to make the pattern more readable:

void display(int dimension) 
{
    int lineNumber = 0, linePosition = 0;
    int numberOfLines = dimension, numberOfCharactersPerLine = dimension;

    for(lineNumber =1;lineNumber<=numberOfLines ; lineNumber++)
    {
         for(linePosition = 1;linePosition <= numberOfCharactersPerLine ;linePosition++)
         {
             if ( linePosition < lineNumber ) {
                 System.out.print(lineNumber);
             } else {
                 System.out.print(linePosition);
             }
         }
         System.out.println();
    }
}

Here we see the instructions with explicitly named variables. If the linePosition (the current character in the line we are deciding to print) is less than the line number we should print the line number, otherwise print the character position.

This is probably better imagined by picturing it as a two dimensional matrix with coordinates and you are filling it in based on those coordinates.

다른 팁

Modify your inner loop. The pattern can be boiled down to an if statement pretty easily. If your k is less than i, you should be printing out I rather than k. So your loops in total will look something like this:

for(int i=1;i<=5;i++)
{
     for(int k=1;k<=5;k++)
     {
         if(k<=i) System.out.print(i);
         else System.out.print(k);
     }
     System.out.println();
}

Don't print out something like i+" " unless you want spaces between every number.

Also you don't need to declare the variables i and k outside the loop if you put their types inside the loop line. That'll declare them at the time of the loop and keep them from persisting afterwards. Though I guess if you need them to persist after being modified by the loops, that's a good way to do it.

For starters... if you changed this line:

for(k=1;k<=5;k++)

To this:

for(k=1;k<=i;k++)

(to get the repeated integers at the beginning of the number)

void display() 
{
    int i,k;
    for(i=1;i<=5;i++)
    {
         for(k=1;k<=5;k++)
         {
             System.out.print(Math.max(i,k));
         }
         System.out.println();
    }
}    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top