Java基础笔记3

关于方法重载 

1.在同一个类中

2.方法名必须相同

3.方法的参数类不同   (和返回值类型没有关系)

如:

public class TestOverLoad{
 public void mOl(int i){
  System.out.println(i*i);
 }
 public void mOl(int i , int j){
  System.out.println(i+2);
 }
 public int mOl(String str){
  System.out.print(str);
  return 0;
 }
}

以上代码来自https://blog.csdn.net/qq_26125865/article/details/78323515

 关于生成随机数(使用种子,通过改变种子来生成不同随机数)

初始化种子seed,通过方法来改变种子的值,循环改变,循环生成。

import java.util.Random;
import java.util.Scanner;

public class Ran_num
{
    static int seed = 2018;
    static Random rtemp1 = new Random();
    static Random rtemp2 = new Random();
    public static void main(String[] args)
    {
        int cs;
        Random rr = new Random(seed);
        Scanner sc = new Scanner(System.in);        
        System.out.println("输入生成个数:");
        cs = sc.nextInt();
        for(int i = 0; i < cs; i++) {
            rr = new Random(seed);
            System.out.println(rr.nextInt()); 
            seed = changeSeed(seed);
        }
    }

    public static int changeSeed(int s) {
        int n = 0;
        int j = 1 + rtemp1.nextInt(100);
        int k = rtemp2.nextInt(3);
        if(k == 0) { 
            n = s * j;
        }
        else if(k == 1) {
            n = s / j;
        }
        else if(k == 2) {
            n = s + j;
        }
        else if(k == 3) {
            n = s - j;
        }
        return n;
    }
    
}
原文地址:https://www.cnblogs.com/leity/p/9783019.html