• Java课程作业01


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

    // 自定义求平方数的静态方法
    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 += " ";
    }

    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(): " + r1.nextBoolean());
    System.out.println("r1.nextInt(): " + r1.nextInt());
    System.out.println("r1.nextDouble(): " + r1.nextDouble());
    System.out.println("r1.nextGaussian(): " + r1.nextGaussian());
    System.out.println("---------------------------");

    Random r2 = new Random(50);
    System.out.println("第二个种子为50的Random对象");
    System.out.println("r2.nextBoolean(): " + r2.nextBoolean());
    System.out.println("r2.nextInt(): " + r2.nextInt());
    System.out.println("r2.nextDouble(): " + r2.nextDouble());
    System.out.println("r2.nextGaussian(): " + r2.nextGaussian());
    System.out.println("---------------------------");

    Random r3 = new Random(100);
    System.out.println("种子为100的Random对象");
    System.out.println("r3.nextBoolean(): " + r3.nextBoolean());
    System.out.println("r3.nextInt(): " + r3.nextInt());
    System.out.println("r3.nextDouble(): " + r3.nextDouble());
    System.out.println("r3.nextGaussian(): " + r3.nextGaussian());


    Random r4 = new Random(System.currentTimeMillis());
    System.out.println("以当前时间为种子的Random对象");
    System.out.println("r3.nextBoolean(): " + r4.nextBoolean());
    System.out.println("r3.nextInt(): " + r4.nextInt());
    System.out.println("r3.nextDouble(): " + r4.nextDouble());
    System.out.println("r3.nextGaussian(): " + 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(" The 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");

  • 相关阅读:
    【WPF】自定义CheckBox复选框
    如何在WPF中引用Windows.System.Forms.Integration
    【转载】wpf DataGrid自动显示行号
    C#网络编程(订立协议和发送文件) Part.4
    C#网络编程(接收文件) Part.5
    状态模式
    C#网络编程(同步传输字符串) Part.2
    我的一个自己写的更新缓存的aop实例
    C#网络编程(基本概念和操作) Part.1
    mssql根据分类查询每个分类前100条数据 表名m_data,分类字段名m_type
  • 原文地址:https://www.cnblogs.com/bangandwolf/p/7663075.html
Copyright © 2020-2023  润新知