注册 登录
编程论坛 JAVA论坛

各位编程大师好! 我是java新手,还请大家多多指教。

huatailong 发布于 2016-10-08 10:35, 1388 次点击
package 小程序;

public class BIANCHENG {
  public static void main(String[] args){
     int x=2,y1;
     double y;
      for(int i=0;i<5;i++) //控制每一行
      { for(int j=4;j>-1;j--) //控制每一行输出几个数
            if(j<=i)
            {y=java.la
                ng.StrictMath.pow(x,j); //控制输出数(一个函数)
             y1=(int)y;
             if(y1<10)
            System.out.print(y1+"  ");  //控制各位之间的距离
             else
            System.out.print(y1+" ");  //控制十位之间的距离
            
            }
            System.out.print("\n");    //换行  
             }
          //热爱计算机技术热爱编程
          System.out.println("热爱计算机技术热爱编程");
      
      
           
   
          }
}

结果是:                                   我要输出的这样的结果:
1                                                    1
2  1                                               1 2 1
4  2 1                                           1 2 4 2 1        :请问该怎么写,或者给出一个思路去下手,当然我知道最简单的方法就是直接 通过输出数字,但是那样显得太没水平了吧!
8  4 2 1
16 8 4 2 1                        
4 回复
#2
ldsh3042016-10-08 22:33
int row = 4;
        for (int i = 1; i <= row; i++) {
            for (int j = 0; j <= row-i; j++) {
                System.out.print("  ");
            }
            for (int j = 1; j < i; j++) {
                System.out.print((int)Math.pow(2, j-1) + " ");
            }
            for (int j = i; j >= 1; j--) {
                System.out.print((int)Math.pow(2, j-1) + " ");
            }
            System.out.println();
        }
#3
huatailong2016-10-10 10:21
回复 楼主 huatailong
感谢你的帮助。 我学java半个月时间,你写的这个程序我已经看懂了。这论坛也是刚前几天在网上搜索到的,以后还会长期在这论坛逛逛。
int row = 4;
        for (int i = 1; i <= row; i++) {                            //这个个花括号与最后一个花括号是一条语句。      
            for (int j = 0; j <= row-i; j++) {             //这个是控制输出的空格个数            
                System.out.print("  ");
            }
            for (int j = 1; j < i; j++) {                     //这个是输出前面 1 12  124   1248
                System.out.print((int)Math.pow(2, j-1) + " ");
            }
            for (int j = i; j >= 1; j--) {                  //这个是输出 1   21    421
                System.out.print((int)Math.pow(2, j-1) + " ");
            }
            System.out.println();        //换行。
        }

      
#4
huatailong2016-10-10 10:23
回复 2楼 ldsh304
回复 楼主 huatailong
感谢你的帮助。 我学java半个月时间,你写的这个程序我已经看懂了。这论坛也是刚前几天在网上搜索到的,以后还会长期在这论坛逛逛。
int row = 4;
        for (int i = 1; i <= row; i++) {                            //这个个花括号与最后一个花括号是一条语句。      
            for (int j = 0; j <= row-i; j++) {             //这个是控制输出的空格个数            
                System.out.print("  ");
            }
            for (int j = 1; j < i; j++) {                     //这个是输出前面 1 12  124   1248
                System.out.print((int)Math.pow(2, j-1) + " ");
            }
            for (int j = i; j >= 1; j--) {                  //这个是输出 1   21    421
                System.out.print((int)Math.pow(2, j-1) + " ");
            }
            System.out.println();        //换行。
        }

      
1