• Java入门——数组和方法


    • 练习题

    1. 求100——1000内的水仙花数

    2.  1 public class Shuixianhua {
       2 
       3     /**
       4      * @param args
       5      */
       6     public static void main(String[] args) {
       7         // TODO Auto-generated method stub
       8         int count=1;
       9         for(int i=100;i<=1000;i++){
      10             int a=(i-i%100)/100;
      11             int b=(i-100*a)/10;
      12             int c=i%10;
      13             int testNum=a*a*a+b*b*b+c*c*c;
      14             if((testNum==i)&(0<a)&(a<10)){
      15                 System.out.println("第"+count+"个水仙花数是"+i);
      16                 count++;
      17             }
      18         }
      19     }
    1. 第1个水仙花数是153
      第2个水仙花数是370
      第3个水仙花数是371
      第4个水仙花数是407
    2. 求三个数的最大值

    3.  1 public class Max {
       2 
       3     /**
       4      * @param args
       5      */
       6     public static void main(String[] args) {
       7         // TODO Auto-generated method stub
       8         int a=12;
       9         int b=32;
      10         int c=321;
      11         if(a<b){
      12             a=b;
      13             if(a<c){
      14                 a=c;
      15             }
      16         }else{
      17             if(a<c)
      18                 a=c;
      19         }
      20         System.out.println("最大值是"+a);
      21     }
      23
    4. 判断某数是否能同时被3、5、7整除

    5.  1 //判断某数是否能同时被3、5、7整除
       2 public class IfDemo2 {
       3 
       4     /**
       5      * @param args
       6      */
       7     public static void main(String[] args) {
       8         // TODO Auto-generated method stub
       9         for(int i=1;i<1000;i++){
      10         if (i%3==0){
      11             if(i%5==0){
      12                 if(i%7==0){
      13                     System.out.println(i);
      14                 }
      15             }
      16         }
      17         }
      18     }
      19 
      20 }
    • 数组

    •  1 package Sep10;
       2 
       3 public class ArrayDemo01 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int [] score=null;
      11         score=new int[3];
      12         System.out.println("score[0]="+score[0]);
      13         System.out.println("score[1]="+score[1]);
      14         System.out.println("score[2]="+score[2]);
      15         System.out.println("******************");
      16         for(int i=0;i<3;i++){
      17             System.out.println("score["+i+"]="+score[i]);
      18         }
      19         
      20     }
      21 
      22 }
      score[0]=0
      score[1]=0
      score[2]=0
      ******************
      score[0]=0
      score[1]=0
      score[2]=0
    • 静态初始化:用{--------}

    •  1 package Sep10;
       2 
       3 public class ArrayDemo04 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int score[]={0,1,3,5,23};
      11         for(int x=0;x<score.length;x++){
      12             System.out.println("score["+x+"]:"+score[x]);
      13         }
      14     }
      15 
      16 }
      score[0]:0
      score[1]:1
      score[2]:3
      score[3]:5
      score[4]:23

      求数组中的最大值和最小值:

    • package Sep10;
      //求数组中的最大值和最小值
      public class ArrayDemo05 {
      
          /**
           * @param args
           */
          public static void main(String[] args) {
              // TODO Auto-generated method stub
              int score[]={12,42,432,43,54,65};
              int max=score[0];
              int min=score[0];
              for(int i=0;i<score.length;i++){
                  if(score[i]>max){
                      max=score[i];
                  }
                  if(score[i]<min){
                      min=score[i];
                  }
              }
              System.out.println("最大值是:"+max);
              System.out.println("最小值是:"+min);
          }
      
      }
      最大值是:432
      最小值是:12
      
    • 冒泡法排列

    •  1 package Sep10;
       2 //数组元素排序
       3 public class ArrayDemo06 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int score[]={4232,123,312,423,43,321};
      11         for(int i=0;i<score.length;i++){
      12             for(int j=i+1;j<score.length;j++){
      13                 if(score[j]<score[i]){
      14                     int middle=score[i];
      15                     score[i]=score[j];
      16                     score[j]=middle;
      17                 }
      18             }
      19         }
      20         for(int i=0;i<score.length;i++){
      21             System.out.println("排列为:"+score[i]);
      22         }
      23     }
      24 
      25 }
      排列为:43
      排列为:123
      排列为:312
      排列为:321
      排列为:423
      排列为:4232
    • 二维数组

          动态初始化

    •  1 package Sep10;
       2 //动态初始化数组
       3 public class ArrayDemo07 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int score[][]=new int[3][4];
      11         score[0][1]=01;
      12         score[1][2]=12;
      13         score[0][2]=02;
      14         score[2][1]=21;
      15         score[2][0]=20;
      16         for(int i=0;i<score.length;i++){
      17             for(int j=0;j<score.length;j++){
      18                 System.out.print(score[i][j]+"	");
      19             }
      20             System.out.println("");
      21         }
      22     }
      23 
      24 }
      0    1    2    
      0    0    12    
      20    21    0    
    • 方法

    • 1:没有形参直接写();2:没有返回值注明void,可以省略return语句;
    •  1 package Sep10;
       2 //调用加减法函数
       3 public class Method01 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int test1=addInt(12,32);
      11         float test2=addFloat(12.2f,32f);
      12         System.out.println(test1);
      13         System.out.println(test2);
      14     }
      15     public static int addInt(int x,int y){
      16         int sum=x+y;
      17         return sum;
      18     }
      19     public static float addFloat(float x,float y){
      20         float sum=x+y;
      21         return sum;
      22     }
      23 }
      44
      44.2
       1 package Sep10;
       2 //方法的重载
       3 public class Method02 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int test1=add(12,32);
      11         float test2=add(12.2f,32f);
      12         System.out.println(test1);
      13         System.out.println(test2);
      14     }
      15     public static int add(int x,int y){
      16         int sum=x+y;
      17         return sum;
      18     }
      19     public static float add(float x,float y){
      20         float sum=x+y;
      21         return sum;
      22     }
      23 
      24 }

      Java中可以使用return直接停止方法的使用

    • 向方法中传递数组

    •  1 package Sep10;
       2 //向方法中传递数组
       3 public class Method03 {
       4 
       5     /**
       6      * @param args
       7      */
       8     public static void main(String[] args) {
       9         // TODO Auto-generated method stub
      10         int[] temp={0,2,1};
      11         fun(temp);//不用加[]
      12         System.out.println(temp[0]);
      13     }
      14     public static void fun(int x[]){
      15         x[0]=6;
      16     }
      17 }
      6
  • 相关阅读:
    java基础(初始化和清理)
    jquery的常用操作(转载)+ 开发中经常犯的错误总结(原创) (不断补充)
    java基础常见错误归纳(值传递和引用传递)
    FormPanel 综合使用 忆江南
    MyEclipse下Jquery代码自动提示 忆江南
    HQL查询 忆江南
    MD5密码保护 忆江南
    FormPanel数据提交 忆江南
    新手上路
    编码总结,以及对BOM的理解
  • 原文地址:https://www.cnblogs.com/BoscoGuo/p/5858397.html
Copyright © 2020-2023  润新知