今天兴致好,把题又做了一遍,留下答案,方便同行面试时查阅,注意别被抓到

免责声明:注意我就是随手一写,对算法效率有要求的慎用

 

这个可以用递归,也可以是普通的写法,这里是普通的写法,注意用的是JDK1.8:

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * 1、一个数字输出阶乘
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        System.out.println("输入一个数字:");
        try(InputStream inputStream = System.in;) {
            Scanner sc = new Scanner(inputStream);
            int number = sc.nextInt();
            System.out.println();
            int result = 1;
            for(int i=number;i>0;i--){
                result = result*i;
            }
            System.out.println("阶乘为:"+result);
        }
        System.exit(0);
    }
}

 

相关文章:

  • 2021-05-07
  • 2021-10-27
  • 2022-12-23
  • 2021-12-25
  • 2022-02-27
  • 2021-08-25
猜你喜欢
  • 2022-12-23
  • 2021-12-01
  • 2021-10-18
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2021-06-15
相关资源
相似解决方案