0

我在使用 while 循环时遇到了一些问题。我的任务是打印带有 2 个输入变量的菱形:1)整数表示中间的行数/最大字符数 2)要打印的字符:伪代码:7 = int $ = char 程序应该打印(并考虑左侧的空格): http: //pastebin.com/cspgz3bA

我的 while 循环看起来很乱。现在他们正在打印(* 模拟空格): http: //pastebin.com/cspgz3bA

我发现将钻石分成两个三角形(上下)是解决这个问题的最简单方法,但是我的代码似乎完全是垃圾......

编辑:: 这仅适用于三角形的上半部分,因此 (rows/2)+1 - 它意味着在完成中间行后停止。

    rows = integerInput;
    maxSpace = integerInput;

    while (currentRow <=((rows/2)+1)) {
        spaceReq = ((maxSpace -1)/2);    // determines spaces required
            while (spaces < spaceReq) {  

                System.out.print("*");
                spaces++;
            }
            while (charPrinted < charReq){
                System.out.print(charInput);
                charPrinted++;

            }               
            currentRow++;
            maxSpace--;
            charReq = charReq +2;
            System.out.println("");
        }

有人可以指出为什么这适用于第一次迭代,但在下一次迭代中中断?

谢谢!

4

1 回答 1

3

charPrinted并且spaces需要重置到0外循环的顶部。

于 2012-04-27T23:07:54.543 回答