動手動腦2

來源:互聯網
上載者:User

標籤:同名   i++   numbers   動手   exit   演算法   format   win   option   

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

import javax.swing.JOptionPane;public class suiji {   public static void main( String args[] )   {      int value;      String output = "";      for ( int i = 1; i <= 1000; i++ ) {         value = 1 + (int) ( Math.random() * 6 );         output += value + "  ";                  if ( i % 50 == 0 )            output += "\n";      }      JOptionPane.showMessageDialog( null, output,         "20 Random Numbers from 1 to 6",         JOptionPane.INFORMATION_MESSAGE );      System.exit( 0 );   }}

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

DK中有許多System.out.println()同名的重載方法,方法名都是print()。:System是java.Lang裡面的一個類。Out是System提供的用於標準輸出的流,在沒有重新導向的情況下,會直接列印到終端,而println這個方式實際上是prinstream類提供的功能。

 

3.

public class MethodOverload {public static void main(String[] args) {System.out.println("The square of integer 7 is " + square(7));System.out.println("\nThe square of double 7.5 is " + square(7.5));}public static int square(int x) {return x * x;}public static double square(double y) {return y * y;}}

  java方法重載:兩個函數功能相同,參數類型不同。

以上代碼展示了java的方法重載,類型不同。

註:滿足以下條件的兩個或多個方法構成“重載”關係:(1)方法名相同;(2)參數類型不同,參數個數不同,或者是參數類型的順序不同。

動手動腦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.