java---随机小结
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                java---随机小结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            package text;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;//  Java隨機
public class Demo {public static void first(String[] args) {Random rand = new Random();for (int i = 0; i < 5; i++) {//System.out.println(Math.random());//隨機小數System.out.println(rand.nextInt(10));//隨機0-9的整數System.out.println(rand.nextInt(10) + 1);//隨機1-10的整數System.out.println(rand.nextInt(11) + 10);//隨機10-20的整數System.out.println(rand.nextBoolean());//隨機真假System.out.println(rand.nextDouble());//隨機小數
        }}
//    隨機日期public static void date(String[] args) {Random rand = new Random();Calendar c = Calendar.getInstance();SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");long start = c.getTime().getTime();System.out.println(sdf.format(c.getTime()));c.set(2000,1,1,0,0,0);long end = c.getTime().getTime();System.out.println(sdf.format(c.getTime()));// long time = Math.round(rand.nextDouble() * (end - start) + start);// c.setTimeInMillis(time);
        System.out.println(sdf.format(c.getTime()));}
//  隨機取出一個字母public static void world(String[] args) {//string stringBuffer stringBuilder的區別StringBuffer str = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890");StringBuffer temp = new StringBuffer("");Random rand = new Random();for (int i = 0; i < 10; i++) {temp.append(str.charAt(rand.nextInt(str.length())));}System.out.println(temp.toString());}
// 隨機取出三個姓名且不重復public static void main(String[] args) {String[] arr = new String[]{"李飛","王偉","關羽","劉備","張飛"};Random rand = new Random();StringBuffer temp = new StringBuffer("");int i = 3;while(i > 0) {String name =arr[rand.nextInt(arr.length)];if(temp.indexOf(name) == -1){temp.append(name+" ");i--;}}System.out.println(temp.toString());}
}  
                        
                        
                        ?
轉載于:https://www.cnblogs.com/zxwen/p/9526384.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的java---随机小结的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 别用这种方式聊天,你都不知道自己是怎么聊
- 下一篇: react跳转url,跳转外链,新页面打
