随机数

本文介绍了Java中字符串的各种操作方法,包括初始化、查找、替换、截取等,并演示了如何使用内置的数学函数进行四舍五入、取整等运算。
package com.hanqi1;

import java.util.Random;

public class Test {

    public static void main(String[] args)
    {
        String str1 = "字符串常量";      //字符串用""   字符用''
        
        String str2 = null;
        
        str2 = new String();
        
        str2 = new String("实例化字符串");
        
        char[] c = new char[]{'a','b','c'};
        
        str2 = new String(c);
        
        str2 = "fhospdowqdjshf";
        
        //字符集 
        
        
        System.out.println("str2.length=" + str2.length());
        
        System.out.println("str2=" + str2);
        
        //查找字符或字符串
        int in = str2.indexOf("bc");
        
        System.out.println("bc ="+in);  //输出的结果为索引值
        
        int la = str2.lastIndexOf("d");
        
        System.out.println("d = "+la);
        
        String newStr = str2.substring(5);
        
        newStr = str2.substring(5,6);
        
        System.out.println("substring()=" + newStr);
        
        str2 = " abcdds ";
        
        //去除前后空格
        System.out.println("去空格 ="+ str2.trim()+"后面");

        //查找替换
        System.out.println("查找替换空格="+str2.replace("","") + "后面");
        
        str2 = "abc,你好";
        
        System.out.println("查找替换空格="+str2.replace("abc","张三") + "");
        
        str2 = "abcde";
        
        //判断字符串开始和结束
        System.out.println("判断起始 = "+str2.startsWith("a"));
        
        System.out.println("判断起始 = "+(str2.indexOf("abc")==0));
    
        System.out.println("判断结束= "+str2.endsWith("de"));
        
        
        str1 = new String("abc");
        str2 = new String("abc");
        
        str2 = "ABC";
        
        System.out.println("判断字符串是否相等="+ (str1 == str2)+"str1="+str1.toUpperCase() +"str2 = "+str2.toLowerCase());
        
        
        System.out.println("判断字符串是否相等="+ str1.equals(str2));
        
        
        
        str2 = "abc#def#ghr#xyz";
        
        String[] array = str2.split("#");
        
        for (int i = 0;i<array.length;i++)
        {
            System.out.println(""+array[i]);
        }
        
        //数学运算
        System.out.println("四舍五入"+Math.round(123.556));
        
        //取上限值
        System.out.println("取上限值"+Math.ceil(123.456));
        
        System.out.println("取下限值"+Math.floor(123.456));
        
        System.out.println("PI="+Math.PI);
        
        
        //取随机数
        System.out.println("随机数="+Math.random());
        System.out.println("随机数="+Math.random());
        System.out.println("随机数="+Math.random());
        System.out.println("随机数="+Math.random());
        
        Random r = new Random();//用时间做种子
        
        r = new Random(1);//随机数种子
        
        System.out.println("随机数="+r.nextInt(100));
        System.out.println("随机数="+r.nextInt(100));
        System.out.println("随机数="+r.nextInt(100));
        System.out.println("随机数="+r.nextInt(100));
        System.out.println("随机数="+r.nextInt(100));
        System.out.println("随机数="+r.nextInt(100));
        System.out.println("随机数="+r.nextInt(100));
        
        
        
        
        
        
        
        
        
        
        
        
    }
    

}

 

转载于:https://www.cnblogs.com/ljxe/p/5052933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值