zoukankan      html  css  js  c++  java
  • 实验三——阶乘

    本实验用4种方法实现阶乘:N!

      即for循环;do--while循环;while循环和递归实现

      在新建的项目内新建一个factorial主类

      输入下列代码

     1 import java.util.Scanner;
     2 public class factorial {
     3 
     4     /**
     5      * @param args
     6      */
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9 
    10         Scanner scanner=new Scanner(System.in);
    11 //构建Scanner类的对象scanner,关联于标准输入流System.in
    12         float sum=1;//用于存储结果
    13         
    14         //for循环实现
    15         System.out.println("for循环实现阶乘");
    16         System.out.print("输入阶乘值:");
    17         int a=scanner.nextInt();
    18         for(int i=1;i<=a;i++){
    19             sum*=i;
    20             System.out.print(i);
    21             if(i<a) System.out.print("*");
    22             }
    23         System.out.println("="+sum);
    24         
    25         //do--while循环实现
    26         System.out.println("
    do--while循环实现阶乘");
    27         int j=1;sum=1;
    28         System.out.print("输入阶乘值:");
    29         int S=scanner.nextInt();
    30         do{
    31             sum*=j;
    32             System.out.print(j);
    33             if(j<S) System.out.print("*");
    34             j++;
    35         }while(j<=S);
    36         System.out.println("="+sum);
    37         
    38         //while循环实现
    39         System.out.println("
    while循环实现阶乘");
    40         System.out.print("输入阶乘值:");
    41         int x=scanner.nextInt();
    42         int l=1;sum=1;
    43         while(l<=x){
    44             sum*=l;
    45             System.out.print(l);
    46             if(l<x) System.out.print("*");
    47             l++;
    48         }
    49         System.out.println("="+sum);
    50         
    51         //递归实现
    52         System.out.println("
    递归实现阶乘");
    53         System.out.print("输入阶乘值:");
    54         int y=scanner.nextInt();
    55         System.out.println("="+jie(y));
    56         
    57         System.out.println("
    以上,结束!");
    58     }
    59     static int jie(int b)
    60     {
    61         if(b==0)
    62             return 1;
    63         else 
    64         {System.out.print(b);
    65         if(b>1) System.out.print("*");
    66             return b*jie(b-1);}
    67         }
    68 
    69 }
    70  
    运行结果:
    for循环实现阶乘
    输入阶乘值:3
    1*2*3=6.0
    do--while循环实现阶乘
    输入阶乘值:4
    1*2*3*4=24.0
    while循环实现阶乘
    输入阶乘值:5
    1*2*3*4*5=120.0
    递归实现阶乘
    输入阶乘值:6
    6*5*4*3*2*1=720
    以上,结束!

     以上,实验结束!

     心得:

      在静态主方法引用函数必须在主类里定义,为静态函数,否则会报错!

         所以上述代码的递归函数名为

            static int jie(int b)

    @勤奋的lu3
  • 相关阅读:
    HDU 4778 内存搜索&amp;如压力
    cocos2dx-2.x CCFileUtils文件管理分析(2)
    开源 自由 java CMS
    1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup
    什么是PV,UV。
    Python爬虫框架Scrapy获得定向打击批量招聘信息
    采用ToolRunner执行Hadoop基本面分析程序
    编辑时snapping的添加
    利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
    由图层判断数据源类型
  • 原文地址:https://www.cnblogs.com/lul3/p/10559873.html
Copyright © 2011-2022 走看看