java---隨機小結

來源:互聯網
上載者:User

標籤:一個   app   nbsp   name   public   gettime   calendar   tin   i++   

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());    }}

 

java---隨機小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.