zoukankan      html  css  js  c++  java
  • 学习联系 Java阶乘相关练习

    题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度

     1 double hou = 0.00008;
     2         for (int i = 1; i > 0; i++)
     3         {
     4             hou = hou * 2;
     5             if (hou > 8848.13)
     6             {
     7                 System.out.println(i);
     8                 break;//跳出循环
     9             }
    10         }

    题目二:计算5的阶乘 5!的结果是?

     1 //阶乘
     2         Scanner a = new Scanner(System.in);
     3         int sum = 1;
     4         System.out.println("请输入一个数字");
     5         int shu = a.nextInt();
     6         for (int i = 1; i <= shu; i++)
     7         {
     8             sum = sum * i ;
     9         }
    10         System.out.println("结果为:" + sum);
    11         

     题目三:计算 1+1/2!+1/3!+1/4!+...1/20!=?
     1 //计算 1+1/2!+1/3!+1/4!+...1/20!=?
     2         Scanner b = new Scanner(System.in);
     3         double he = 0;
     4         System.out.println("请输入数字:");
     5         double shuzi = b.nextInt();
     6         for (int i = 1; i <= shuzi; i++)
     7         {
     8             double jiecheng = 1;
     9             //求i的阶乘
    10             for (int j = 1; j <= i; j++)
    11             {
    12                 jiecheng = jiecheng*j/i;
    13             }
    14             he = he + jiecheng;//累加
    15         }
    16 
    17         System.out.println("阶乘的和为"+he);
    18         
    19         
    20         
    21         }
    22 
    23     }

  • 相关阅读:
    docker生产——容器通信
    .net core集成JWT(基础)
    JWT基本概念
    MySQL数据更新
    MySQL查询练习2
    C语言程序设计之字符串处理
    MySQL查询练习
    博客文章搬迁
    C语言程序设计之 数组2020-10-28
    Java方法重载浅谈
  • 原文地址:https://www.cnblogs.com/zhoudi/p/5477670.html
Copyright © 2011-2022 走看看