Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2019/12/28(土)
- // 階乗
- import java.util.Scanner;
- public class Fact {
- public static void main(String[] args) {
- // 1!=1 2!=2×1 3!=3×2×1 …
- // 0!は1とする
- int lastNum;
- int result;
- Scanner scanner = new Scanner(System.in);
- System.out.println("0から10までのいずれかの整数を入力");
- while (true) {
- lastNum = scanner.nextInt();
- if (lastNum < 0 || lastNum > 10) {
- System.out.println("0から10までの整数を入力");
- } else {
- break;
- }
- }
- if (lastNum == 0) {
- result = 1;
- } else {
- result = 1;
- for (int i = 1; i <= lastNum; i++) {
- result *= i;
- }
- }
- System.out.println(lastNum + "の階乗は" + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement