求一个整数的阶乘 5! = 5*4*3*2*1

import java.util.Scanner;

public class Test12 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int sum=1;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int k = sc.nextInt();
		for (int i = 1; i <= k; i++) {
			sum=sum*i;
		}
		System.out.println(k+"的阶乘为:"+sum);
	}
}

你可能感兴趣的:(JAVA学习之旅)