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
我感谢任何能为我解决这个问题的人。
答案 0 :(得分:1)
好吧,由于原始代码不是最好的代码,我已经重写了它:
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 :(得分:1)
逐步完成每个流程。 timesPrintedInLine
首先为0,因此在Print()
中它会打印一个新行。当timesPrintedInLine
为1时,1不是5的倍数,因此System.out.print
将打印char
中correctChar()
返回的Print()
;因为1 /(1 * 4)不等于1,所以打印B
。 2和3也是如此。
当timesPrintedInLine
为4时,我们再次转到correctChar()
;这次打印A
。
timesPrintedInLine
现在变为5,因此会打印一个新行,linesPrinted
现在等于5/4或1。
对于timesPrintedInLine
= 6和7,{6}从6/4和7/4等于1打印{。}}。
从此时开始,A
将始终大于7.随后,timesPrintedInLine
/(1 * 4)永远不会等于1,因此这就是timesPrintedInLine
始终打印的原因(B
是5的倍数时除外;在这种情况下,会打印一个新行,timesPrintedInLine
等于linesPrinted
/ 4)。
问题来自您的timesPrintedInLine
声明,因此请将其更改为:
if
答案 2 :(得分: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"
}
}
答案 3 :(得分: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
中的条件已更改,以适应后续更改。 (你的仅限于第一次迭代)如果您需要解释,请询问。