java課堂疑問解答與思考2

來源:互聯網
上載者:User

標籤:next   必須   ora   line   rand   log   port   源碼   個數   

問題一

編寫一個方法,使用以上演算法產生指定數目(比如1000個)的隨機整數。

答:Xn+1=(7^5*Xn)mod(2^31-1)

程式源碼:

import java.util.Random;

import java.util.Scanner;

public class RandomCount

{

  public static void main(String[] args)

  {

    Random rand=new Random();

    System.out.print("請輸入想要輸出隨機數的個數:");

    Scanner in=new Scanner(System.in);

    int n,x;

    n=in.nextInt();

    x=rand.nextInt(100);

    for(int i=0;i<n;i++)

    {

      x=(7^5*x+0)%2147483647;

      System.out.print(x+" ");

       if ( i % 10 == 0 )

                  System.out.println();

    }

}

結果:

 

問題二

請看以下代碼,你發現了有什麼特殊之處嗎?

答:這叫作方法重載,對於方法重載,參數名必須相同,參數類型可以不同,參數個數可以不同,參數類型的順序可以不同。但傳回值本能作為判斷條件。

 

問題三

查看一下JDK中System.out.println()方法,你發現了什嗎?

答:System.out.println()中有很多函數,但方法名都是print()。使用了方法的重載。

 

java課堂疑問解答與思考2

聯繫我們

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