标签:rand 超过 port double format exti over 当前时间 es2017
1.阶乘
import java.math.BigInteger;
import java.util.Scanner;
public class CalculateN {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("请输入N:");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
System.out.println(number+"!="+calculateN2(number));
}
public static long calculateN(int n) {
if(n==1 || n==0){
return 1;
}
return n*calculateN(n-1);
}
public static BigInteger calculateN2(int n) {
if(n==1 || n==0){
return BigInteger.valueOf(1);
}
return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
}
}
2.平方数的静方法
public class SquareInt {
public static void main(String[] args) {
int result;
for (int x = 1; x <= 10; x++) {
result = square(x);
// Math库中也提供了求平方数的方法
// result=(int)Math.pow(x,2);
System.out.println("The square of " + x + " is " + result + "\n");
}
}
// 自定义求平方数的静态方法
public static int square(int y) {
return y * y;
}
}
3.随机数生产
import javax.swing.JOptionPane;
public class RandomInt {
public static void main( String args[] )
{
int value;
String output = "";
for ( int i = 1; i <= 20; i++ ) {
value = 1 + (int) ( Math.random() * 6 );
output += value + " ";
if ( i % 5 == 0 )
output += "\n";
}
JOptionPane.showMessageDialog( null, output,
"20 Random Numbers from 1 to 6",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
4.使用random类生产随机数
import java.util.Random;
public class TestSeed
{
public static void main(String[] args)
{
Random r1 = new Random(50);
System.out.println("第一个种子为50的Random对象");
System.out.println("r1.nextBoolean():\t" + r1.nextBoolean());
System.out.println("r1.nextInt():\t\t" + r1.nextInt());
System.out.println("r1.nextDouble():\t" + r1.nextDouble());
System.out.println("r1.nextGaussian():\t" + r1.nextGaussian());
System.out.println("---------------------------");
Random r2 = new Random(50);
System.out.println("第二个种子为50的Random对象");
System.out.println("r2.nextBoolean():\t" + r2.nextBoolean());
System.out.println("r2.nextInt():\t\t" + r2.nextInt());
System.out.println("r2.nextDouble():\t" + r2.nextDouble());
System.out.println("r2.nextGaussian():\t" + r2.nextGaussian());
System.out.println("---------------------------");
Random r3 = new Random(100);
System.out.println("种子为100的Random对象");
System.out.println("r3.nextBoolean():\t" + r3.nextBoolean());
System.out.println("r3.nextInt():\t\t" + r3.nextInt());
System.out.println("r3.nextDouble():\t" + r3.nextDouble());
System.out.println("r3.nextGaussian():\t" + r3.nextGaussian());
Random r4 = new Random(System.currentTimeMillis());
System.out.println("以当前时间为种子的Random对象");
System.out.println("r3.nextBoolean():\t" + r4.nextBoolean());
System.out.println("r3.nextInt():\t\t" + r4.nextInt());
System.out.println("r3.nextDouble():\t" + r4.nextDouble());
System.out.println("r3.nextGaussian():\t" + r4.nextGaussian());
}}
5.重载
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;
}
}
6.CalculateN示例程序中的BUG
import java.math.BigInteger;
import java.util.Scanner;
public class CalculateN {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("请输入N:");
Scanner scanner=new Scanner(System.in);
int number=scanner.nextInt();
System.out.println(number+"!="+calculateN2(number));
}
public static long calculateN(int n) {
if(n==1 || n==0){
return 1;
}
return n*calculateN(n-1);
}
public static BigInteger calculateN2(int n) {
if(n==1 || n==0){
return BigInteger.valueOf(1);
}
return BigInteger.valueOf(n).multiply(calculateN2((n-1)));
}
}
由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多位数,这将导致错误的处理结果。
7.比较两个浮点数
double i = 0.0001;
double j = 0.00010000000000000001;
System.out.println(i==j); //输出:true
计算机不能精确地表达浮点数(特殊形式的除外),因此,当需要比较两个浮点数是否相等时,应该比较其差的绝对值是否在某个允许范围之内即可。
if ( Math.Abs(i - j) < 1e-10 )
System.out.println("true");
else
System.out.println("false");
标签:rand 超过 port double format exti over 当前时间 es2017
原文地址:http://www.cnblogs.com/bangandwolf/p/7663075.html