码迷,mamicode.com
首页 > 其他好文 > 详细

动手动脑2

时间:2018-10-14 19:06:14      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:同名   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

标签:同名   i++   numbers   动手   exit   算法   format   win   option   

原文地址:https://www.cnblogs.com/NCLONG/p/9787102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!