动手动脑2

  1.随机数

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 public class Test {
 4     public static void main(String [] args) {
 5         System.out.println("请输入产生随机数的位数");
 6         Scanner sc=new Scanner(System.in);
 7         int n=sc.nextInt();
 8         for(int i=1;i<=n;i++) {
 9     Random a=new Random();
10     int x=a.nextInt(1000);
11     System.out.println(x);
12         }
13 }
14 }

 

2.指定代码特殊之处

 1 public class MethodOverload {
 2 
 3     public static void main(String[] args) {
 4         System.out.println("The square of integer 7 is " + square(7));
 5         System.out.println("\nThe square of double 7.5 is " + square(7.5));
 6     }
 7 
 8     public static int square(int x) {
 9         return x * x;
10     }
11 
12     public static double square(double y) {
13         return y * y;
14     }
15 }

我觉得这是运用了函数的重载。两个函数的名字是一样的。但是在定义函数的类型时一个是int型一个是double型。在调用函数时,square(7)自动调用int类型的函数。而square(7.5)则自动调用double类型的函数。

posted @ 2018-10-14 23:25  birdmmxx  阅读(112)  评论(0编辑  收藏  举报