我在while循环中遇到了一些问题。我有一个任务打印一个菱形2个输入变量: 1)整数表示行数/最大中间字符数 2)要打印的字符: 伪代码: 7 = int $ = char 程序应打印(并在左侧占空间): http://pastebin.com/cspgz3bAWhile循环绘制钻石
我的while循环似乎是一团糟。现在他们正在打印(*模拟空格): http://pastebin.com/cspgz3bA
我发现,钻石分离成两个三角形(上&下)要解决这个最简单的方法,但是我的代码似乎是完整的垃圾...
编辑::这是为只有三角形的上半部分,因此(行/ 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("");
}
有人可以指出为什么这适用于第1次迭代,但打破了下一个呢?
谢谢!
你愿意发布一个更完整的代码吗?输入/输出?理想情况下,张贴一个SSCCE http://sscce.org – 2012-04-27 23:02:02
对不起,我应该做到这一点 - jpm给了我没有它,但我寻找的答案:) – Numpty 2012-04-27 23:13:25