2019.09.23课堂总结

一、动手动脑1

编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

代码如下:

 1 import java.util.Scanner;
 2 
 3 public class Random {
 4     private static Scanner sc = new Scanner(System.in);
 5 
 6     public static void main(String[] args) {
 7         System.out.println("请输入想要生成的随机数的个数:");
 8         int n = 0;
 9         n = sc.nextInt();
10         System.out.println("生成的随机数为:");
11         int x = 10;
12         for (int i = 1; i <= n; i++) {
13             x = (7 ^ 5 * x + 0) % 2147483647;
14             System.out.print(x + "	");
15             if (i % 5 == 0) {
16                 System.out.println("");
17             }
18         }
19     }
20 }

运行截图:

 二、动手动脑2

请看以下代码,你发现了有什么特殊之处吗?

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

上述代码展示了Java方法重载的特性,满足以下条件的两个或多个方法构成重载关系:

(1)方法名相同;

(2)参数个数不同或参数类型不同或参数类型顺序不同。

原文地址:https://www.cnblogs.com/best-hym/p/11588732.html