9.22(代码测试)
CalculateN.java
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 5 public class CalculateN { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 System.out.print("������N��"); 12 Scanner scanner=new Scanner(System.in); 13 int number=scanner.nextInt(); 14 System.out.println(number+"!="+calculateN2(number)); 15 16 } 17 18 public static long calculateN(int n) { 19 if(n==1 || n==0){ 20 return 1; 21 } 22 23 return n*calculateN(n-1); 24 } 25 26 public static BigInteger calculateN2(int n) { 27 if(n==1 || n==0){ 28 return BigInteger.valueOf(1); 29 } 30 return BigInteger.valueOf(n).multiply(calculateN2((n-1))); 31 } 32 }
运行结果
CompareFloatNumber.java
1 public class CompareFloatNumber { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 //compare(); 8 compare2(); 9 10 } 11 12 private static void compare() { 13 double i = 0.0001; 14 double j = 0.00010000000000000001; 15 System.out.println(i==j); //�����true 16 } 17 private static void compare2() { 18 double i = 0.0001; 19 double j = 0.00010000000000000001; 20 if(Math.abs(i-j)<1e-10){ 21 System.out.println("true"); 22 } 23 else 24 { 25 System.out.println("false"); 26 } 27 28 } 29 30 }
运行结果
MethodOverload.java
1 package ceshi;// 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("\nThe 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 }
运行结果
RandomInt.java
1 package ceshi;// RandomInt.java 2 // Shifted, scaled random integers 3 import javax.swing.JOptionPane; 4 5 public class RandomInt { 6 public static void main( String args[] ) 7 { 8 int value; 9 String output = ""; 10 11 for ( int i = 1; i <= 20; i++ ) { 12 value = 1 + (int) ( Math.random() * 6 ); 13 output += value + " "; 14 15 if ( i % 5 == 0 ) 16 output += "\n"; 17 } 18 19 JOptionPane.showMessageDialog( null, output, 20 "20 Random Numbers from 1 to 6", 21 JOptionPane.INFORMATION_MESSAGE ); 22 23 System.exit( 0 ); 24 } 25 }
运行结果
RollDie.java
1 package ceshi;// RollDie.java 2 // Roll a six-sided die 6000 times 3 import javax.swing.*; 4 5 public class RollDie { 6 public static void main( String args[] ) 7 { 8 int frequency1 = 0, frequency2 = 0, 9 frequency3 = 0, frequency4 = 0, 10 frequency5 = 0, frequency6 = 0, face; 11 12 // summarize results 13 for ( int roll = 1; roll <= 6000; roll++ ) { 14 face = 1 + (int) ( Math.random() * 6 ); 15 16 switch ( face ) { 17 case 1: 18 ++frequency1; 19 break; 20 case 2: 21 ++frequency2; 22 break; 23 case 3: 24 ++frequency3; 25 break; 26 case 4: 27 ++frequency4; 28 break; 29 case 5: 30 ++frequency5; 31 break; 32 case 6: 33 ++frequency6; 34 break; 35 } 36 } 37 38 JTextArea outputArea = new JTextArea( 7, 10 ); 39 40 outputArea.setText( 41 "Face\tFrequency" + 42 "\n1\t" + frequency1 + 43 "\n2\t" + frequency2 + 44 "\n3\t" + frequency3 + 45 "\n4\t" + frequency4 + 46 "\n5\t" + frequency5 + 47 "\n6\t" + frequency6 ); 48 49 JOptionPane.showMessageDialog( null, outputArea, 50 "Rolling a Die 6000 Times", 51 JOptionPane.INFORMATION_MESSAGE ); 52 System.exit( 0 ); 53 } 54 }
运行结果
SquareInt.java
1 package ceshi; 2 3 public class SquareInt { 4 5 public static void main(String[] args) { 6 int result; 7 8 for (int x = 1; x <= 10; x++) { 9 result = square(x); 10 // Math����Ҳ�ṩ����ƽ�����ķ��� 11 // result=(int)Math.pow(x,2); 12 System.out.println("The square of " + x + " is " + result + "\n"); 13 } 14 } 15 16 // �Զ�����ƽ�����ľ�̬���� 17 public static int square(int y) { 18 return y * y; 19 } 20 }
运行结果
TestMath.java
1 package ceshi; 2 3 4 public class TestMath 5 { 6 public static void main(String[] args) 7 { 8 /*---------��������������---------*/ 9 //������ת���Ƕ� 10 System.out.println("Math.toDegrees(1.57)��" + Math.toDegrees(1.57)); 11 //���Ƕ�ת��Ϊ���� 12 System.out.println("Math.toRadians(90)��" + Math.toRadians(90)); 13 //���㷴���ң����صĽǶȷ�Χ�� 0.0 �� pi ֮�䡣 14 System.out.println("Math.acos(0.3)��" + Math.acos(1.2)); 15 //���㷴���ң����صĽǶȷ�Χ�� -pi/2 �� pi/2 ֮�䡣 16 System.out.println("Math.asin(0.8)��" + Math.asin(0.8)); 17 //���㷴���У����صĽǶȷ�Χ�� -pi/2 �� pi/2 ֮�䡣 18 System.out.println("Math.atan(2.3)��" + Math.atan(2.3)); 19 //�����������ҡ� 20 System.out.println("Math.cos(1.57)��" + Math.cos(1.57)); 21 //����ֵ��˫�����ҡ� 22 System.out.println("Math.cosh(1.2 )��" + Math.cosh(1.2 )); 23 //�������� 24 System.out.println("Math.sin(1.57 )��" + Math.sin(1.57 )); 25 //����˫������ 26 System.out.println("Math.sinh(1.2 )��" + Math.sinh(1.2 )); 27 //������������ 28 System.out.println("Math.tan(0.8 )��" + Math.tan(0.8 )); 29 //����˫������ 30 System.out.println("Math.tanh(2.1 )��" + Math.tanh(2.1 )); 31 //���������� (x, y) ת���ɼ����� (r, thet));���������ý� theta�� 32 System.out.println("Math.atan2(0.1, 0.2)��" + Math.atan2(0.1, 0.2)); 33 /*---------������ȡ������---------*/ 34 //ȡ��������С��Ŀ��������������� 35 System.out.println("Math.floor(-1.2 )��" + Math.floor(-1.2 )); 36 //ȡ�������ش���Ŀ��������С������ 37 System.out.println("Math.ceil(1.2)��" + Math.ceil(1.2)); 38 //��������ȡ�� 39 System.out.println("Math.round(2.3 )��" + Math.round(2.3 )); 40 /*---------�����dz˷���������ָ������---------*/ 41 //����ƽ������ 42 System.out.println("Math.sqrt(2.3 )��" + Math.sqrt(2.3 )); 43 //������������ 44 System.out.println("Math.cbrt(9)��" + Math.cbrt(9)); 45 //����ŷ���� e ��n���ݡ� 46 System.out.println("Math.exp(2)��" + Math.exp(2)); 47 //���� sqrt(x2��" +y2)��û���м���������硣 48 System.out.println("Math.hypot(4 , 4)��" + Math.hypot(4 , 4)); 49 // ���� IEEE 754 ���Ĺ涨�����������������������㡣 50 System.out.println("Math.IEEEremainder(5 , 2)��" + Math.IEEEremainder(5 , 2)); 51 //����˷� 52 System.out.println("Math.pow(3, 2)��" + Math.pow(3, 2)); 53 //������Ȼ���� 54 System.out.println("Math.log(12)��" + Math.log(12)); 55 //�������Ϊ 10 �Ķ����� 56 System.out.println("Math.log10(9)��" + Math.log10(9)); 57 // �ز����� 1 ֮�͵���Ȼ������ 58 System.out.println("Math.log1p(9)��" + Math.log1p(9)); 59 /*---------�����Ƿ�����ص�����---------*/ 60 //�������ֵ�� 61 System.out.println("Math.abs(-4.5)��" + Math.abs(-4.5)); 62 //���Ÿ�ֵ�����ش��еڶ������������ŵĵ�һ����������� 63 System.out.println("Math.copySign(1.2, -1.0)��" + Math.copySign(1.2, -1.0)); 64 //���ź������������Ϊ 0���� 0������������� 0���� 1.0���������С�� 0���� -1.0�� 65 System.out.println("Math.signum(2.3)��" + Math.signum(2.3)); 66 /*---------�����Ǵ�С��ص���������---------*/ 67 //�ҳ����ֵ 68 System.out.println("Math.max(2.3 , 4.5)��" + Math.max(2.3 , 4.5)); 69 //������Сֵ 70 System.out.println("Math.min(1.2 , 3.4)��" + Math.min(1.2 , 3.4)); 71 //���ص�һ�������͵ڶ�������֮�����һ���������ڵĸ������� 72 System.out.println("Math.nextAfter(1.2, 1.0)��" + Math.nextAfter(1.2, 1.0)); 73 //���ر�Ŀ�����Դ�ĸ����� 74 System.out.println("Math.nextUp(1.2 )��" + Math.nextUp(1.2 )); 75 //����һ��α���������ֵ���ڵ��� 0.0 ��С�� 1.0�� 76 System.out.println("Math.random()��" + Math.random()); 77 } 78 }
运行结果
TestRandom.java
1 package ceshi; 2 3 import java.util.*; 4 5 public class TestRandom 6 { 7 public static void main(String[] args) 8 { 9 Random rand = new Random(); 10 System.out.println("rand.nextBoolean()��" + rand.nextBoolean()); 11 byte[] buffer = new byte[16]; 12 rand.nextBytes(buffer); 13 System.out.println(Arrays.toString(buffer)); 14 //����0.0~1.0֮���α���double�� 15 System.out.println("rand.nextDouble()��" + rand.nextDouble()); 16 //����0.0~1.0֮���α���float�� 17 System.out.println("rand.nextFloat()��" + rand.nextFloat()); 18 //����ƽ��ֵ�� 0.0�������� 1.0��α��˹�� 19 System.out.println("rand.nextGaussian()��" + rand.nextGaussian()); 20 //����һ������long����ȡֵ��Χ��α������� 21 System.out.println("rand.nextInt()��" + rand.nextInt()); 22 //����0~26֮���α������� 23 System.out.println("rand.nextInt(26)��" + rand.nextInt(26)); 24 //����һ������long����ȡֵ��Χ��α������� 25 System.out.println("rand.nextLong()��" + rand.nextLong()); 26 } 27 }
运行结果
TestSeed.java
1 package ceshi; 2 3 import java.util.Random; 4 5 public class TestSeed 6 { 7 public static void main(String[] args) 8 { 9 Random r1 = new Random(50); 10 System.out.println("��һ������Ϊ50��Random����"); 11 System.out.println("r1.nextBoolean():\t" + r1.nextBoolean()); 12 System.out.println("r1.nextInt():\t\t" + r1.nextInt()); 13 System.out.println("r1.nextDouble():\t" + r1.nextDouble()); 14 System.out.println("r1.nextGaussian():\t" + r1.nextGaussian()); 15 System.out.println("---------------------------"); 16 17 Random r2 = new Random(50); 18 System.out.println("�ڶ�������Ϊ50��Random����"); 19 System.out.println("r2.nextBoolean():\t" + r2.nextBoolean()); 20 System.out.println("r2.nextInt():\t\t" + r2.nextInt()); 21 System.out.println("r2.nextDouble():\t" + r2.nextDouble()); 22 System.out.println("r2.nextGaussian():\t" + r2.nextGaussian()); 23 System.out.println("---------------------------"); 24 25 Random r3 = new Random(100); 26 System.out.println("����Ϊ100��Random����"); 27 System.out.println("r3.nextBoolean():\t" + r3.nextBoolean()); 28 System.out.println("r3.nextInt():\t\t" + r3.nextInt()); 29 System.out.println("r3.nextDouble():\t" + r3.nextDouble()); 30 System.out.println("r3.nextGaussian():\t" + r3.nextGaussian()); 31 32 33 Random r4 = new Random(System.currentTimeMillis()); 34 System.out.println("�Ե�ǰʱ��Ϊ���ӵ�Random����"); 35 System.out.println("r3.nextBoolean():\t" + r4.nextBoolean()); 36 System.out.println("r3.nextInt():\t\t" + r4.nextInt()); 37 System.out.println("r3.nextDouble():\t" + r4.nextDouble()); 38 System.out.println("r3.nextGaussian():\t" + r4.nextGaussian()); 39 } 40 }
运行结果
VariableArgumentsTest.java
1 package ceshi; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 7 8 public class VariableArgumentsTest{ 9 10 public static double max(double...values) 11 { 12 double largest=Double.MIN_VALUE; 13 for (double v:values) 14 if(v>largest) largest=v; 15 return largest; 16 } 17 18 public static void main(String args[]) { 19 20 System.out.println("Max:"+max(1,11,300,2,3)); 21 22 } 23 }
运行结果