9.22


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)));
    }
}

public class CompareFloatNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //compare();
        compare2();

    }

    private static void compare() {
        double i = 0.0001;
        double j = 0.00010000000000000001;
        System.out.println(i==j);  //�����true
    }
    private static void compare2() {
        double i = 0.0001;
        double j = 0.00010000000000000001;
        if(Math.abs(i-j)<1e-10){
            System.out.println("true");  
        }
        else
        {
            System.out.println("false");
        }
        
    }

}
// MethodOverload.java
// Using overloaded methods

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;
    }
}
// RandomInt.java
// Shifted, scaled random integers
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 );
   }
}
// RollDie.java
// Roll a six-sided die 6000 times
import javax.swing.*;

public class RollDie {
   public static void main( String args[] )
   {
      int frequency1 = 0, frequency2 = 0,
          frequency3 = 0, frequency4 = 0,
          frequency5 = 0, frequency6 = 0, face;
   
      // summarize results
      for ( int roll = 1; roll <= 6000; roll++ ) {
         face = 1 + (int) ( Math.random() * 6 );
   
         switch ( face ) {
            case 1:
               ++frequency1;
               break;
            case 2:
               ++frequency2;
               break;
            case 3:
               ++frequency3;
               break;
            case 4:
               ++frequency4;
               break;
            case 5:
               ++frequency5;
               break;
            case 6:
               ++frequency6;
               break;
         }
      }

      JTextArea outputArea = new JTextArea( 7, 10 );

      outputArea.setText(
         "Face\tFrequency" +
         "\n1\t" + frequency1 +
         "\n2\t" + frequency2 +
         "\n3\t" + frequency3 +
         "\n4\t" + frequency4 +
         "\n5\t" + frequency5 +
         "\n6\t" + frequency6 );

      JOptionPane.showMessageDialog( null, outputArea,
         "Rolling a Die 6000 Times",
         JOptionPane.INFORMATION_MESSAGE );
      System.exit( 0 );
   }
}
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;
    }
}
public class TestMath
{
    public static void main(String[] args) 
    {
        /*---------��������������---------*/
        //������ת���Ƕ�
        System.out.println("Math.toDegrees(1.57)��" + Math.toDegrees(1.57)); 
        //���Ƕ�ת��Ϊ����
        System.out.println("Math.toRadians(90)��" + Math.toRadians(90));
        //���㷴���ң����صĽǶȷ�Χ�� 0.0 �� pi ֮�䡣
        System.out.println("Math.acos(0.3)��" + Math.acos(1.2)); 
        //���㷴���ң����صĽǶȷ�Χ�� -pi/2 �� pi/2 ֮�䡣 
        System.out.println("Math.asin(0.8)��" + Math.asin(0.8)); 
        //���㷴���У����صĽǶȷ�Χ�� -pi/2 �� pi/2 ֮�䡣 
        System.out.println("Math.atan(2.3)��" + Math.atan(2.3)); 
        //�����������ҡ�
        System.out.println("Math.cos(1.57)��" + Math.cos(1.57)); 
        //����ֵ��˫�����ҡ� 
        System.out.println("Math.cosh(1.2 )��" + Math.cosh(1.2 )); 
        //��������
        System.out.println("Math.sin(1.57 )��" + Math.sin(1.57 )); 
        //����˫������
        System.out.println("Math.sinh(1.2 )��" + Math.sinh(1.2 ));
        //������������
        System.out.println("Math.tan(0.8 )��" + Math.tan(0.8 )); 
        //����˫������
        System.out.println("Math.tanh(2.1 )��" + Math.tanh(2.1 )); 
        //���������� (x, y) ת���ɼ����� (r, thet));���������ý� theta�� 
        System.out.println("Math.atan2(0.1, 0.2)��" + Math.atan2(0.1, 0.2));
        /*---------������ȡ������---------*/
        //ȡ��������С��Ŀ���������������
        System.out.println("Math.floor(-1.2 )��" + Math.floor(-1.2 )); 
        //ȡ�������ش���Ŀ��������С������
        System.out.println("Math.ceil(1.2)��" + Math.ceil(1.2)); 
        //��������ȡ��
        System.out.println("Math.round(2.3 )��" + Math.round(2.3 )); 
        /*---------�����dz˷���������ָ������---------*/
        //����ƽ������
        System.out.println("Math.sqrt(2.3 )��" + Math.sqrt(2.3 )); 
        //������������ 
        System.out.println("Math.cbrt(9)��" + Math.cbrt(9)); 
        //����ŷ���� e ��n���ݡ�
        System.out.println("Math.exp(2)��" + Math.exp(2)); 
        //���� sqrt(x2��" +y2)��û���м���������硣
        System.out.println("Math.hypot(4 , 4)��" + Math.hypot(4 , 4));
        // ���� IEEE 754 ��׼�Ĺ涨�����������������������㡣
        System.out.println("Math.IEEEremainder(5 , 2)��" + Math.IEEEremainder(5 , 2));
        //����˷�
        System.out.println("Math.pow(3, 2)��" + Math.pow(3, 2));
        //������Ȼ����
        System.out.println("Math.log(12)��" + Math.log(12)); 
        //�������Ϊ 10 �Ķ�����
        System.out.println("Math.log10(9)��" + Math.log10(9)); 
        // �ز����� 1 ֮�͵���Ȼ������ 
        System.out.println("Math.log1p(9)��" + Math.log1p(9)); 
        /*---------�����Ƿ�����ص�����---------*/
        //�������ֵ��
        System.out.println("Math.abs(-4.5)��" + Math.abs(-4.5));
        //���Ÿ�ֵ�����ش��еڶ������������ŵĵ�һ�����������
        System.out.println("Math.copySign(1.2, -1.0)��" + Math.copySign(1.2, -1.0));
        //���ź������������Ϊ 0���򷵻� 0������������� 0���򷵻� 1.0���������С�� 0���򷵻� -1.0��
        System.out.println("Math.signum(2.3)��" + Math.signum(2.3)); 
        /*---------�����Ǵ�С��ص���������---------*/
        //�ҳ����ֵ
        System.out.println("Math.max(2.3 , 4.5)��" + Math.max(2.3 , 4.5));
        //������Сֵ 
        System.out.println("Math.min(1.2 , 3.4)��" + Math.min(1.2 , 3.4));
        //���ص�һ�������͵ڶ�������֮�����һ���������ڵĸ�������
        System.out.println("Math.nextAfter(1.2, 1.0)��" + Math.nextAfter(1.2, 1.0));
        //���ر�Ŀ�����Դ�ĸ�����
        System.out.println("Math.nextUp(1.2 )��" + Math.nextUp(1.2 ));
        //����һ��α���������ֵ���ڵ��� 0.0 ��С�� 1.0��
        System.out.println("Math.random()��" + Math.random());
    }
}
import java.util.*;

public class TestRandom
{
    public static void main(String[] args) 
    {
        Random rand = new Random();
        System.out.println("rand.nextBoolean()��" + rand.nextBoolean());
        byte[] buffer = new byte[16];
        rand.nextBytes(buffer);
        System.out.println(Arrays.toString(buffer));
        //����0.0~1.0֮���α���double��
        System.out.println("rand.nextDouble()��" + rand.nextDouble());
        //����0.0~1.0֮���α���float��
        System.out.println("rand.nextFloat()��" + rand.nextFloat());
        //����ƽ��ֵ�� 0.0����׼���� 1.0��α��˹��
        System.out.println("rand.nextGaussian()��" + rand.nextGaussian());
        //����һ������long����ȡֵ��Χ��α�������
        System.out.println("rand.nextInt()��" + rand.nextInt());
        //����0~26֮���α�������
        System.out.println("rand.nextInt(26)��" + rand.nextInt(26));
        //����һ������long����ȡֵ��Χ��α�������
        System.out.println("rand.nextLong()��" +  rand.nextLong());
    }
}
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()); 
    }
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class VariableArgumentsTest{
    
    public static double max(double...values)
    {
        double largest=Double.MIN_VALUE;
        for (double v:values)
            if(v>largest) largest=v;
        return largest;
    }

    public static void main(String args[]) {
    
         System.out.println("Max:"+max(1,11,300,2,3));
            
    }
}
// TowersOfHanoi.java
// Towers of Hanoi solution with a recursive method.
public class TowersOfHanoi
{
   // recursively move disks between towers
   public static void solveTowers( int disks, int sourcePeg, 
      int destinationPeg, int tempPeg )
   {
      // base case -- only one disk to move
      if ( disks == 1 )
      {
         System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );
         return;
      } // end if

      // recursion step -- move (disk - 1) disks from sourcePeg
      // to tempPeg using destinationPeg
      solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );

      // move last disk from sourcePeg to destinationPeg
      System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );

      // move ( disks - 1 ) disks from tempPeg to destinationPeg
      solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
   } // end method solveTowers

   public static void main( String[] args )
   {
      int startPeg = 1; // value 1 used to indicate startPeg in output
      int endPeg = 3; // value 3 used to indicate endPeg in output
      int tempPeg = 2; // value 2 used to indicate tempPeg in output
      int totalDisks = 3; // number of disks
      
      // initial nonrecursive call: move all disks.
      solveTowers( totalDisks, startPeg, endPeg, tempPeg );
   } // end main
} // end class TowersOfHanoi

 

 

 

posted @ 2023-09-22 17:28  白卓冉  阅读(22)  评论(0)    收藏  举报