首页 > 代码库 > JAVA基础针对自己薄弱环节总结02(循环)

JAVA基础针对自己薄弱环节总结02(循环)

循环

A:水仙花。

classShuiXianHua

{

       public static void main(String[] args)

       {

              for(int i=101;i<1000;i++)

              {

                     int ge=i%10;

                     int shi=i/10%10;

                     int bai=i/100%10;

                     if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i)

                            System.out.print(i+"");

              }

       }

}

结果:

技术分享

B:统计叠多少次,能叠成珠穆朗玛峰的高度。

classZhuMuLangMa

{

       public static void main(String[] args)

       {

              int end=884800;

              int start=1;

              int count=0;

              do

              {

                     start*=2;

                     count++;

              }while (start<=end);

             

              System.out.println("叠"+count+"次超过珠穆朗玛峰的高度");

       }

}

结果:

技术分享

C:正三角形

classZhengSanJiao

{

       public static void main(String[] args)

       {

              for(int i=0;i<5;i++)

              {

                     for(int j=i;j<=5;j++)

                     {

                            System.out.print("");

                     }

                     //System.out.println();

                     for(int j=0;j<=i;j++)

                     {

                            System.out.print("*");

                     }

                     for(int j=0;j<i;j++)

                     {

                            System.out.print("*");

                     }

                     System.out.println();

              }

                    

       }

}

结果:

技术分享

D:倒三角形

classDaoSanJiao

{

       public static void main(String[] args)

       {

              for(int i=0;i<5;i++)

              {

                     for(int j=0;j<i;j++)

                     {

                            System.out.print("");

                     }

                     //System.out.println();

                     for(int j=i;j<5;j++)

                     {

                            System.out.print("*");

                     }

                     for(int j=i;j<4;j++)

                     {

                            System.out.print("*");

                     }

                     System.out.println();

              }

                    

       }

}

结果:

技术分享

数组

A定义格式:

一维

int[]arr=new int[6];

intarr[]=new int[6];          

int[]arr=new int[]{1,2,3,4};

int[]arr={1,2,3,4};

 

二维

A:int[][]arr = new int[3][2];

B:int[][]arr = new int[3][];

C:int[][]arr = {{1,2,3},{4,5},{6,7,8,9}};

B应用:

一维数组的查找

classFind

{

       public static void main(String[] args)

       {

             

              int[] arr=new int[]{1,2,3,4};

              System.out.println(findArr(arr,4));

             

       }

       public static int findArr(int[] arr,int x)

       {

              for(int i=0;i<arr.length;i++)

              {

                     if(arr[i]==x)

                            return i;

              }

              return -1;

       }

}

结果:

技术分享

二维数组的查找

classShuiXianHua

{

       public static void main(String[] args)

       {

             

              int[][] arr ={{1,2,3},{4,5},{6,7,8,9}};

              sortArr(arr);

             

       }

       public static void sortArr(int[][]arr)

       {

              for(int i=0;i<arr.length;i++)

              {

                     for(intj=0;j<arr[i].length;j++)

                     {

                            System.out.print(arr[i][j]+"");

                     }     

                     System.out.println();

              }

             

       }

}

结果:

技术分享

Java内存图

栈:局部变量,用完马上释放

堆:存放new出来的东西,有地址值,会初始化默认值。有垃圾回收机制

方法区:存放方法。String常量池。静态区()

本地方法区:跟系统相关的方法

寄存器:由CPU运行

 

JAVA基础针对自己薄弱环节总结02(循环)