01Java方法
一:动手动脑
1.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数
package reserve;import java.util.Scanner;import java.util.Arrays;import java.util.Random;publicclass Main{publicstaticvoid main(String[] args) { Random random
=new Random(); Scanner sc
=new Scanner(System.in); System.out.println(
"请输入产生随机数的个数:");int count=sc.nextInt();byte[] buffer=newbyte[count]; random.nextBytes(buffer);
System.out.println(Arrays.toString(buffer));
}
}
package hu;import java.util.Random;import java.util.Scanner;publicclass Hu {publicstaticvoid main(String[] args) {Scanner in
= new Scanner(System.in);int m = (int)Math.pow(7,5);int n = (int)Math.pow(2, 31) - 1;int c = 0;System.out.print(
"请输入要产生随机数的数量:");int count = in.nextInt();for(int i = 0;i<count;i++){
Random x1
= new Random();int x3 = x1.nextInt(10);int x2 = (m* x3+c)%n;System.out.println(x2);
if (i%5==0)//进行换行操作{System.out.println("");}
}
}
}
2.请看一下代码,你发现了什么特殊之处吗?
package reserve;import java.util.Scanner;import java.util.Arrays;import java.util.Random;publicclass Main{publicstaticvoid 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)); }
publicstaticint square(int x) {return x*x; }
publicstaticdouble square(double y) {return y*y; }
}
代码测试截图:
展现了Java中,方法的重载(方法名相同,参数类型,参数个数,或者参数顺序不同)
只有计算出来结果才能判断出类型。(方法的返回值不作为方法重载的判断条件)
以上是 01Java方法 的全部内容, 来源链接: utcz.com/z/509790.html