zoukankan      html  css  js  c++  java
  • 方法

    方法前不加static

    static表示的静态。

    如果此函数是静态的,会在程序初始化过程中直接进行内存加载,此方法内的所有方法内容必须是静态的,否则会报错静态方法引用动态变量。方法调用:“类名.方法”

    如果此函数是动态的,那么只有在调用的时候才会被主动加载一次,之后释放内存。方法调用:必须先实例化出来一个类,之后再通过实例化类名+方法的形式调用。

    public class StaticTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            int n=2;
            int result;
            result=new NoStatic().square(n);
            System.out.println(result);
        }
    
        
    }
    class NoStatic
    {
        public int square (int n)
        {
            return n*n;
        }
    }

    结果截图

    编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

    import java.util.*;
    public class PureRandom {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Scanner in=new Scanner(System.in);
            int n;
            System.out.println("输入要生成的随机数个数");
            n=in.nextInt();
            //纯随机数生成
            int seed=1;
            int c=0;
            seed=(7^5*seed+c)%(2^31-1);
            for(int i=0;i<n;i++)
                {
                seed=(7^5*seed+c)%(2^100);
                System.out.println(seed);
                }
        }
    
    }

    程序截图:

     

    square方法重载,因为它满足以下三个重载条件

    1.必须是同一个类 2.方法名(也可以叫函数)一样 3.参数类型不一样或参数数量不一样

    杨辉三角形与组合数公式

    组合数公式

    import java.util.*;
    public class Combination {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            //组合数公式
             Scanner in = new Scanner(System.in);
             int n,k;
             int flag=1;//判断输入数据是否正确
             System.out.println("输入元素个数");
             n=in.nextInt();
             System.out.println("输入要取出的元素个数");
             k=in.nextInt();
             if(k>n) 
                 {
                 flag=0;
                 System.out.println("error");
                 }
             if(flag==1)
             System.out.println("组合数结果为"+factorial(n)/(factorial(k)*factorial(n-k)));
        }
        //阶乘函数
        public static int factorial(int n)
        {
            if(n<0) System.out.println("error");
            if(n==0||n==1) return 1;
            else return n*factorial(n-1);
        }
    }

    递推公式

    import java.util.*;
    public class Recurrence {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            //递推公式
                int n,k;
                int flag=1;//判断输入数据是否正确
                Scanner in = new Scanner(System.in);
                 System.out.println("输入元素个数");
                 n=in.nextInt();
                 System.out.println("输入要取出的元素个数");
                 k=in.nextInt();
                 if(k>n) 
                 {
                 flag=0;
                 System.out.println("error");
                 }
                 if(flag==1)
                     System.out.println("组合数结果为"+((factorial(n-1)/(factorial(k-1)*factorial(n-k)))+(factorial(n-1)/(factorial(k-1)*factorial(n-k-1)))));
        }
    
         //阶乘函数
            public static int factorial(int n)
            {
                if(n<0) System.out.println("error");
                if(n==0||n==1) return 1;
                else return n*factorial(n-1);
            }
    }

    杨辉三角

    import java.util.*;
    public class triangle {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //杨辉三角
            int n,k;
            int flag=1;//判断输入数据是否正确
            Scanner in = new Scanner(System.in);
             System.out.println("输入元素个数");
             n=in.nextInt();
             System.out.println("输入要取出的元素个数");
             k=in.nextInt();
             if(k>n) 
             {
             flag=0;
             System.out.println("error");
             }
             if(flag==1)
                 System.out.println("组合数结果为"+( (factorial(n-1)/(factorial(k-1)*factorial(n-k-1))) + (factorial(n-1)/(factorial(k)*factorial(n-k))) ));
        }
        //阶乘函数
        public static int factorial(int n)
        {
            if(n<0) System.out.println("error");
            if(n==0||n==1) return 1;
            else return n*factorial(n-1);
        }
    }

    程序截图:

    递归编程解决汉诺塔问题。用Java实现

    public class Hanoi {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
              int startPeg = 1; //代表startPeg
              int endPeg = 3; //代表endPeg
              int tempPeg = 2; //代表temPeg
              int totalDisks = 3; //disk总数
              //盘子移动
              solveTowers( totalDisks, startPeg, endPeg, tempPeg );
            
        }
         public static void solveTowers( int disks, int sourcePeg, 
                  int destinationPeg, int tempPeg )
               {
                 
                  if ( disks == 1 )
                  {
                     System.out.printf( "
    %d --> %d", sourcePeg, destinationPeg );
                     return;
                  } 
    
                 //移动初始盘
                  solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );
    
                 //初始盘到目标盘
                  System.out.printf( "
    %d --> %d", sourcePeg, destinationPeg );
    
                 //临时防治盘到目标盘
                  solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
               } 
    }

    程序截图:

     使用递归方式判断某个字串是否是回文( palindrome )

    import java.util.*;
    public class palindrome {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Scanner in=new Scanner(System.in);
            String Str;
            System.out.println("随意输入一段字符");
            Str=in.next();
            if(PalidJudge(Str)==true)
                System.out.println("这段字符是回文");
            else System.out.println("这段字符不是回文");
        }
        
    
        //判断是否为回文数
        public static boolean PalidJudge(String S)
        {
            int l=S.length();
            if(l<=1) return true;//
            if(S.charAt(0)!=S.charAt(l-1)) return false;
            return PalidJudge(S.substring(1,l-1));
        }
    }

    程序截图:

  • 相关阅读:
    CRM 2013 相关下载 / 2013-10-11
    四十,Importer extension 主要(/imports)
    三十九,GeoWebCache 播种和截断(/seed)
    三十八,GeoWebCache 重新加载(/reload)
    三十七,GeoWebCache 质量截断(/statistics)
    三十六,GeoWebCache 质量截断(/masstruncate)
    三十五,GeoWebCache 图层(/layers)
    三十四,GeoWebCache 主页(/index)
    三十三,GeoWebCache 网格集(/gridsets)
    三十二,GeoWebCache 全局配置(/global)
  • 原文地址:https://www.cnblogs.com/clueless/p/5966143.html
Copyright © 2011-2022 走看看