For a cycle for printing letters

public class LetterPrint {
    int totalLines;
    int consecLines;
    int timesPrintedinLine;
    int linesPrinted;
    char currentChar;
    LetterPrint(int totalLines, int consecLines){
        this.totalLines = totalLines;
        this.consecLines = consecLines;
    }
    void beginPrinting(){
        for(timesPrintedinLine = 0; linesPrinted<=totalLines; timesPrintedinLine++)
        {
            Print();
        }
    }
    public char correctChar(){
        if(timesPrintedinLine/(consecLines*4) == 1)
            return currentChar = 'A';
        else 
            return currentChar = 'B';

    }
    void Print(){
        if (timesPrintedinLine%5 !=0)
            System.out.print(correctChar());
        else{
            System.out.println();
            linesPrinted = timesPrintedinLine/4;
        }

    }
}

Can someone help me understand why when created with LetterPrint letterPrinter = new LetterPrint (6,1); "this item prints

BBBA
AABB
BBBB
BBBB
BBBB
BBBB 

but not

AAAA
BBBB
AAAA
BBBB
AAAA
BBBB

I appreciate anyone who can clean this for me.

+4
source share
4 answers

Well, since the source code is not "best", I rewrote it:

public class LetterPrint
{
    private final static int charsInLine = 4;

    private final int totalLines;
    private final int consecLines;

    public LetterPrint(int totalLines, int consecLines)
    {
        this.totalLines = totalLines;
        this.consecLines = consecLines;
    }

    public void beginPrinting()
    {
        for (int lineNumber = 0; lineNumber < totalLines; lineNumber++)
        {
            char currentChar = getChar(lineNumber);
            for (int charNumber = 0; charNumber < charsInLine; charNumber++)
            {
                System.out.print(currentChar);
            }
            System.out.println();
        }
    }

    private char getChar(int lineNumber)
    {
        if ((lineNumber / consecLines) % 2 == 0)
        {
            return 'A';
        }
        return 'B';
    }
}
+1
source

Go through each process. timesPrintedInLinefirst 0, so in Print()it will print a new line. When timesPrintedInLineequal to 1, 1 is not a multiple of 5, therefore it System.out.printwill print char, returned from correctChar()to Print(); because 1 / (1 * 4) is not equal to 1, it Bis printed. The same thing happens for 2 and 3.

timesPrintedInLine 4, correctChar(); A.

timesPrintedInLine 5, , linesPrinted 5/4 1.

timesPrintedInLine= 6 7, A 6/4 7/4 1.

timesPrintedInLine 7. timesPrintedInLine/(1 * 4) 1, B ( , timesPrintedInLine 5, , linesPrinted timesPrintedInLine/4).

if, :

if(timesPrintedinLine/(consecLines*5) % 2 == 0)
+1

-, timesPrintedinLine linesPrinted . System.out

:

public class LetterPrint {
   private static final int CHARS_BY_CONSLINES = 4; 

   private int totalLines;
   private int consecLines;

   LetterPrint(int totalLines, int consecLines){
       this.totalLines = totalLines;
       this.consecLines = consecLines;
   }

   void beginPrinting(){
       String text;
       for(int rows = 0; rows < totalLines; rows++){
           text = "";
           for (int cols = 0; cols < consecLines*CHARS_BY_CONSLINES; cols++) {
               text += printChar(rows);
           }
           System.out.println(text);
       }
   } 

   void printChar(int row){
       if (row%2==0)
           return "A";
       else
           return "B"
   }

}

+1

.

public class LetterPrint {
    int totalLines;
    int consecLines;
    int timesPrintedinLine;
    int linesPrinted;
    char currentChar;
    LetterPrint(int totalLines, int consecLines){
        this.totalLines = totalLines;
        this.consecLines = consecLines;
    }
    void beginPrinting(){
        for(timesPrintedinLine = 1; linesPrinted<=totalLines; timesPrintedinLine++)
        {
            Print();
        }
    }
    public char correctChar(){
        if((timesPrintedinLine/(consecLines*4+1)) %2  == 0)
            return currentChar = 'A';
        else 
            return currentChar = 'B';

    }
    void Print(){
        if (timesPrintedinLine%5 !=0)
            System.out.print(correctChar());
        else{
            System.out.println();
            linesPrinted = timesPrintedinLine/4;
        }

    }

    public static void main(String ar[])
    {
     LetterPrint LETTERPRINTER = new LetterPrint(6,1);
     LETTERPRINTER.beginPrinting();
    }
}

, .

AAAA
BBBB
AAAA
BBBB
AAAA
BBBB

:

  • for 1, .
  • correctChar . ( )

, , .

+1

All Articles