zoukankan      html  css  js  c++  java
  • Java探索之旅(4)——方法和Random&Math类

    1.基本知识点

       ❶方法在C++里面称为函数。调用方法时,应该类型兼容——即不需显式类型换即可将形参传递给实参。
       ❷形参的改变不影响实参的值。
       ❸Java注重模块化设计和自顶向下的设计

    2.Math类

       Math隐式导入,故不需要import。常用函数如下:

        ❶正三角和反三角函数:sin(),cos(),tan(),asin(),acos(),atan()。类型均为double,输入弧度
        ❷角度弧度转换函数:toRadians(),toDegrees()。参数和返回类型均为double
        ❸指数函数和对数函数:exp(),log(),log10(),pow(),sqrt()
        ❹取整函数:上整ceil(),下整floor,取最近整数(距离相等返回奇数)rint()
           round(float x)=(int)floor(x+0.5),round(double x)=(long) floor(x+0.54)
        ❺两者之间最大min(),最小max(),输入2数类型可相异;绝对值abs()

    3.生成随机数Random

        ❶0.0<=Math.random()<1.0。

              Math.random()*100;  //返回位于[0 100)的随机数,但是不等于100
              'a'+Math.random()*('z'-'a'+1); //随机返回‘a’-'z'之间的字母 
    

    随机返回[a,b]之间的数或者字符(a+Math.random()*(b-a+1))。如果没有+1,则返回值的区间为[a,b)。使用方法如下面代码。

        ❷Random类

            利用java.util.Random类定义的随机数产生对象。如果种子相同,产生的随机数也相同,默认以当前时间为种子。使用方法如下:

    java.util.Random data=new java.util.Random(3);//使用种子3产生随机数,默认以当前时间为种子
    int a=data.nextInt();
    int b=data.nextInt(1000);//(0,1000)之间的随机整数
    double c=data.nextDouble();//(0,1.0)之间的随机数
    float d=data.nextFloat();//(0.0F,1.0F)之间随机数
    boolean e=data.nextBoolean();//随机false or true

    4.代码

        ❶调用函数计算任意10进制数的16进制形式
        ❷调用扩展类生成100个'a'-'z'之间的随机字符

    package function_study;
    import java.util.Scanner;
    public class function_study {
    	public static void main(String[] args) 
    	{
    	    Scanner input=new Scanner(System.in);
    	    System.out.print("输入一个大于0的数---");
    	    int decimal=input.nextInt();
    	    String hex="";
    	    while(decimal!=0)//int值不能作为boolen类型,此处不能直接使用decimal
    	    {
    	        hex=decimal2hex(decimal%16)+hex;
    	        decimal/=16; 
    	    }
    	    hex="0X "+hex;
    	    System.out.println("10进制转换为16进制的结果是---"+hex);
    	    input.close();
    	    for(int i=0;i<100;i++)<span style="font-family: Arial, Helvetica, sans-serif;">//随机生成a-z之间的100个字符</span>
    
    	    	if((i+1)%10==0)
    	    		System.out.println(RandomCharacter.getRandomLower());
    	    	else
    	    		System.out.print(RandomCharacter.getRandomLower());
    	}
    	public static char decimal2hex(int decimal)
    	{
    		if(decimal>=0&&decimal<=9)
    			return (char)('0'+decimal);
    		else
    			return (char)(decimal-10+'A');
    	}
    }
    
    package function_study;
    public class RandomCharacter {
    public static char getRandomCharacter(char ch1,char ch2)
        {return (char)(ch1+Math.random()*(ch2-ch1+1));}
    public static char getRandomLower()
        {return getRandomCharacter('a','z');}
    public static char getRandomHigh()
        {return getRandomCharacter('A','Z');}
    public static char getRandomDigit()
        {return getRandomCharacter('0','9');}
    public static char getRandomCharacter()
        {return getRandomCharacter('u0000','uFFFF');}
    }

  • 相关阅读:
    ZZ 一些有意思的算法代码
    ff 怎样让新打开的标签就放在当前页面的右边
    111
    Windows平台下GO语言编译器(GOwindows)
    My Bookmarks
    使用googleperftools的tcmalloc
    memcached安装及测试
    erlang 入门(1)
    MS UI Automation
    twisted: echo server
  • 原文地址:https://www.cnblogs.com/engineerLF/p/5393099.html
Copyright © 2011-2022 走看看