📄 printpyramid.java
字号:
/**
* @(#)PrintPyramid.java
*
*
* @author
* @version 1.00 2009/3/3
*/
//打印金字塔图案,其中的数字为对称2^n数列.
//import javax.swing.JOptionPane;
public class PrintPyramid {
/**
* Creates a new instance of <code>PrintPyramid</code>.
*/
public PrintPyramid() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//打印行数
int numberOfLines=8;
//打印
for(int row=1;row<=numberOfLines;row++)
{
//打印左边的空白,每个为4个空格.
for(int column=1;column<=numberOfLines-row;column++)
System.out.print(" ");
// System.out.print(1);
//打印每行左边递增的幂序列:从1……2^numberOfLines-1,共n个.
for(int i=1;i<=row;i++)
{
//计算幂,注意是n-1次方.
int myPow=1;
for(int j=2;j<=i;j++)
{
myPow*=2;
}
//调整空白以对齐
if(myPow>=100)
System.out.print(" "+myPow);
else if(myPow>=10)
System.out.print(" "+myPow);
else
System.out.print(" "+myPow);
}
//打印每行右边的幂序列,从2^n-2……1.
for(int i=row-1;i>=1;i--)
{
//计算幂
int myPow=1;
for(int j=2;j<=i;j++)
{
myPow*=2;
}
//处理空白以对齐
if(myPow>=100)
System.out.print(" "+myPow);
else if(myPow>=10)
System.out.print(" "+myPow);
else
System.out.print(" "+myPow);
}
//换行,准备打印下一行.
System.out.print('\n');
}
return ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -