我有嵌套循环打印模式的问题。虽然大多数教程显示金字塔矿山更复杂。java嵌套循环模式
我需要打印基于用户输入的行的#,例如以下:
2行:
o //\\.
o// \\.
4行:
o //\\.
o // \\.
o // \\.
o// \\.
这是我到目前为止所尝试的:
import java.util.Scanner;
public class Homework {
public static void main(String[] args) {
// int size = 2;
int noOfRows = 3;
//Scanner sc = new Scanner(System.in);
System.out.println("How Many Rows You Want In Your Pyramid?");
// int noOfRows = sc.nextInt();
System.out.println("Here Is Your Pyramid");
for (int i = 1; i <= noOfRows; i++) {
System.out.print("o");
//Printing i*2 spaces at the beginning of each row
for (int j = noOfRows * 2 + 1; j >= 1; j--) {
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = noOfRows + 1; j >= 1; j--) {
System.out.print("//");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = noOfRows + 2; j <= noOfRows; j++) {
System.out.print("\\" + "\\" + ".");
}
System.out.println();
//Incrementing the rowCount
}
System.out.println(); // NEWLINE
}
}
输出:
How Many Rows You Want In Your Pyramid?
Here Is Your Pyramid
o ////////
o ////////
o ////////
输出不打印金字塔。我如何修复我的代码以获得预期的结果?欢迎任何建议。
您的第二行是长于你的第二排例子中的第二排是你真正的意思吗? – bhspencer
啊,我搞砸了,应该看起来像2行,但都排队,生病尝试编辑。 –
请解释你正在得到什么“问题”,包括你得到的错误输出或错误堆栈跟踪。 – bcsb1001