我应该写一个嵌套循环,输出以下:for循环的Java ...创建金字塔
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
我应该使用两种方法。
主要方法只应该从用户获取所需的行数。 我应该写称为printPyramid另一种方法是:
- 拥有的行数
- 打印与行数
- 没有返回值金字塔。
到目前为止,我有:
import java.util.Scanner;
public class Pyramid {
//Getting number of rows, calling printPyramid in main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Get user input = number of lines to print in a pyramid
System.out.println("Enter the number of lines to produce: ");
int numLines = input.nextInt();
//call method:
printPyramid();
}//End of main method
//Making a new method...
public static void printPyramid (int numLines) {
int row;
int col;
row = 0;
col = 0;
for (row = 1; row <= numLines; row++){
//print out n-row # of spaces
for (col = 1; col <= numLines-row; col++){
System.out.print(" ");
}
//print out digits = 2*row-1 # of digits printed
for (int dig= 1; dig <= 2*row-1; dig++){
System.out.print(row + " ");
}
System.out.println();
}//end of main for loop
}//end of printPyramid
}//end of class
我得到的错误,我无法弄清楚如何得到它正确地打印出来。 我相信方法搞砸了?
你能发表你的错误吗?如果你对此一无所知,那么很难解决一个错误。 **和**是这个作业吗? – elyashiv
*我收到错误... *您有什么错误? – Pigueiras
请打破你的问题是具体的,显示与任何相关的代码和调试错误打破的单个功能/隔间 – Ozzy