302班

          java突擊隊
          posts - 151, comments - 74, trackbacks - 0, articles - 14
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          數字的似三角形的特殊顯示

          Posted on 2007-04-14 02:21 停留的風 閱讀(339) 評論(0)  編輯  收藏 所屬分類: Java程序集合

          如圖所示:

          源代碼如下:
          import javax.swing.JOptionPane;
          public class PrintPyramid
          {
           public static void main(String[] args)
           {
            String input=JOptionPane.showInputDialog(null,"Enter the number of

          lines:","Example",JOptionPane.QUESTION_MESSAGE);
            int numberOfLines=Integer.parseInt(input);

            if(numberOfLines<1||numberOfLines>15)
            {
              System.out.print("You must enter a numberi from 1 to 15!");
              System.exit(0);
            }
            for(int row=1;row<=numberOfLines;row++)
            {
              for(int column=1;column<=numberOfLines-row;column++)   
              System.out.print("  ");
              
                     for(int num=row;num>=1;num--)   
                     System.out.print((num>=10)?" "+num:" "+num);

             
              for(int num=2;num<=row;num++)   
              System.out.print((num>=10)?" "+num:" "+num); 
                                 //start a new line  
              System.out.println();
            }
           }
          }


          備注:

          print   就是輸出你要打印的,但是不包含換行。   
          println除了輸出外,還能自動換行。

          主站蜘蛛池模板: 马龙县| 浦江县| 大同县| 昭通市| 拉萨市| 涪陵区| 上林县| 冀州市| 都江堰市| 镶黄旗| 石嘴山市| 敦化市| 道孚县| 定兴县| 湘西| 余江县| 昌乐县| 浦北县| 石阡县| 乌苏市| 岳池县| 成都市| 铜鼓县| 阿拉善左旗| 馆陶县| 肇州县| 东乡| 益阳市| 长岭县| 安宁市| 昆山市| 合川市| 辽阳县| 晋宁县| 东丽区| 泗水县| 施甸县| 尉犁县| 上高县| 江油市| 苍梧县|