Java基礎學習筆記 -- 6(Scanner、Random)

來源:互聯網
上載者:User

標籤:scanner   random   next   nextline   

1. Scanner類

    JDK提供的控制台掃描類,用於讀取控制台輸入的資訊。使用前需要import java.util.Scanner; (匯入包)。

    使用:

     // 建立對象 : Scanner sca = new Scanner(System.in); 

     // 擷取字串 : String str1 = sca.next();   //可以以 空格、斷行符號符分行符號(\r\n) 表示結束

                                String str2 = sca.nextLine();  //只能以\r\n表示結束

     // 擷取整數: int num = sca.nextInt(); 

     // 擷取小數: double d = sca.nextDouble(); 

    案例8:

        650) this.width=650;" style="width:661px;height:413px;" title="8B3Q%P85F)2S3.png" src="http://s3.51cto.com/wyfs02/M01/6C/C1/wKiom1VRdTDTT5z0AAJpObTW1T4630.jpg" width="721" height="457" alt="wKiom1VRdTDTT5z0AAJpObTW1T4630.jpg" />

     案例9:

        650) this.width=650;" style="width:660px;height:405px;" title="9E.png" src="http://s3.51cto.com/wyfs02/M01/6C/BC/wKioL1VRdueDraC2AAKhTQkS-wc236.jpg" width="740" height="437" alt="wKioL1VRdueDraC2AAKhTQkS-wc236.jpg" />

    輸出結果: 

        650) this.width=650;" title="999AJ4QY9_@)DRF.png" src="http://s3.51cto.com/wyfs02/M01/6C/C1/wKiom1VRdY2yZO56AACuDVI8n2Q231.jpg" alt="wKiom1VRdY2yZO56AACuDVI8n2Q231.jpg" />

 

2. Random類

    JDK提供的擷取隨機數的類,用於擷取隨機小數、整數、boolean類型等。使用前需要import java.util.Random;

    使用:

     // 建立對象 : Random random = new Random(); 

     // 擷取範圍內的隨機整數: int num = ran.nextInt(n); // 取值 [0, n)

    // 擷取隨機的boolean值:boolean flag = ran.nextBoolean();

    // 擷取隨機的小數:double d = ran.nextDouble();

    案例10:

        650) this.width=650;" title="10.png" src="http://s3.51cto.com/wyfs02/M02/6C/C1/wKiom1VRdevSgt-JAAHX9PlivoI537.jpg" alt="wKiom1VRdevSgt-JAAHX9PlivoI537.jpg" />

 

本文出自 “永遠年輕” 部落格,請務必保留此出處http://kingkongzhao.blog.51cto.com/6319491/1650551

Java基礎學習筆記 -- 6(Scanner、Random)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.