zoukankan      html  css  js  c++  java
  • 【第三次JAVA课,java语法基础】课件总结

    动手动脑1

      首先是个算法,常见的随机数产生器。

      

       从网络上我了解到这个算法的名称叫 线性同余,用这个算式可以得出在统计学上均匀的伪随机数,也是相当经典而运用广泛的随机数产生器了。

      在这个式子里,各个系数的范围:

          模m, m > 0

          系数a, 0 < a < m

          增量c, 0 <= c < m

          原始值(种子) 0 <= X(0) < m

    根据这个式子,我设计了一个产生随机数的算法,其中,m=231-1,c=0,a=75,X0=系统时间(毫秒级)。

    public class MyRand
    {
        private long seed;
        private long random_number;
    
        public MyRand()
        {
            seed = System.currentTimeMillis();
            rand(seed);
        }
    
        private void rand(long seed)
        {
            random_number = (16807 * seed) % Integer.MAX_VALUE;
        }
    
        public long Random()
        {
            rand(random_number);
            return random_number;
        }
    
        public static void main(String[] args)
        {
            MyRand a = new MyRand();
            for (int i = 0; i < 1000; i++)
            {
                System.out.println("[" + (i + 1) + "]" + a.Random());
            }
    
        }
    
    }

      个人体感是随机的,课件上介绍是232-2次后才会重复。

    动手动脑2

      

    public class MethodOverload {
    
        public static void main(String[] args) {
            System.out.println("The square of integer 7 is " + square(7));
            System.out.println("
    The square of double 7.5 is " + square(7.5));
        }
    
        public static int square(int x) {
            return x * x;
        }
    
        public static double square(double y) {
            return y * y;
        }
    }

      

      这段代码特殊之处在于在Main函数里调用了两个同名函数,这两函数通过重载的方式保证不会有重名的冲突。

      方法重载是指“两个或多个方法名相同的函数可以被编辑器识别调用”,编译器会识别函数的签名来使“名字”相同的函数有了不同的“昵称”,只要参数类型、参数个数、参数顺序有一个不同那就会使编译器能够识别并重载,但方法返回值不是方法重载的判断条件!

    递归

      利用递归处理回文判断。

    import java.util.Scanner;
    
    public class Palindrome
    {
        private String string;
    
        public Palindrome(String a)
        {
            string = a;
        }
    
        public boolean isPalindrome()
        {
            if (string.length() <= 1)
                return true;
            else
            {
                if (string.substring(string.length() - 1).equals(string.substring(0, 1)))
                {
                    string = string.substring(1, string.length() - 1);
                    isPalindrome();
                } else
                    return false;
            }
            return true;
        }
    
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            String a = in.nextLine();
            Palindrome b = new Palindrome(a);
            System.out.println(b.isPalindrome());
            ;
        }
    }
  • 相关阅读:
    OS-lab4
    OS-lab3
    OS-lab2
    OS-lab1
    OO第四单元总结
    OO第三单元总结
    OO第二单元总结
    HTTP_POST
    实习日志1(2020.7.27-2020.9.31)
    Web app ------ 从Servlet读取Json数据并显示,生成历史数据曲线图
  • 原文地址:https://www.cnblogs.com/limitCM/p/9783474.html
Copyright © 2011-2022 走看看