Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2019/12/28(土)、29(日)
- // 階乗
- 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 factNum;
- int result;
- Scanner scanner = new Scanner(System.in);
- System.out.println("0から10までのいずれかの整数を入力");
- while (true) {
- factNum = scanner.nextInt();
- if (factNum < 0 || factNum > 10) {
- System.out.println("0から10までの整数を入力");
- } else {
- break;
- }
- }
- result = 1;
- for (int i = 1; i <= factNum; i++) { // i=0の時、このfor文は実行されない
- result *= i;
- }
- System.out.println(factNum + "の階乗は" + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement