標籤:同名 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