gpt4 book ai didi

java - 打印字母的for循环

转载 作者:搜寻专家 更新时间:2023-11-01 02:43:43 24 4
gpt4 key购买 nike

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;
}

}
}

谁能帮我看看为什么,当用“LetterPrint letterPrinter = new LetterPrint(6,1);”创建时这个对象正在打印

BBBA
AABB
BBBB
BBBB
BBBB
BBBB

而不是

AAAA
BBBB
AAAA
BBBB
AAAA
BBBB

我感谢任何能为我解决这个问题的人。

最佳答案

好吧,因为原始代码不是“最好的”,所以我重写了它:

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';
}
}

关于java - 打印字母的for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com