阶乘计算器

Factorial Calculator

如何让程序执行新的或重复的操作或要求用户再次输入一个数字并知道它的阶乘。

import java.util.Scanner;

public class Loops {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        int num, num2 = 1, x, i;

        System.out.println("-------------Factorial Calculator-------------");
        System.out.print("Enter a positive integer: ");
        num = input.nextInt();

        if (num > 0) {
            System.out.print(num + "! = ");
            for (i = 1; i <= num; i++) {
                if(i < num){
                    System.out.print(i + " x ");
                    num2 = num2 * i;
                }
                if(i == num){
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of " + num + " is " + num2);
                    System.out.println();
                }
            }
        }else if (num < 0) {
            System.out.println("Please input a valid integer. Program stopped.");
        }
    }
}

当用户不输入正数时使用 while 循环:

System.out.print("Enter a positive integer: ");
num = input.nextInt();
while(num<0) {
    System.out.println("Please input a valid integer. Program stopped.");
    num = input.nextInt();
}

代码:

    import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int num, num2 = 1, x, i;

        System.out.println("-------------Factorial Calculator-------------");
        System.out.print("Enter a positive integer: ");
        num = input.nextInt();
        input.nextLine();
        while (num < 0) {
            System.out.println("Please input a valid integer. Program stopped.");
            System.out.print("Enter a positive integer: ");
            num = input.nextInt();
        }
        if (num > 0) {
            System.out.print(num + "! = ");
            for (i = 1; i <= num; i++) {
                if (i < num) {
                    System.out.print(i + " x ");
                    num2 = num2 * i;
                }
                if (i == num) {
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of " + num + " is " + num2);
                    System.out.println();
                }
            }
        }
    }
}

使用while循环

import java.util.Scanner;

public class Loops {
    static boolean readInput = true;
    public static void main(String[] args) {
        while(readInput) calculateFactorial();
    }
}

private static void calculateFactorial() {
    Scanner input = new Scanner(System.in);

    int num, num2 = 1, x, i;

    System.out.println("-------------Factorial Calculator-------------");
    System.out.print("Enter a positive integer: ");
    num = input.nextInt();
    if(num <= 0) {
        readInput = false;
        return;
    }

    if (num > 0) {
        System.out.print(num + "! = ");
        for (i = 1; i <= num; i++) {
            if(i < num){
                System.out.print(i + " x ");
                num2 = num2 * i;
            }
            if(i == num){
                System.out.print(i);
                num2 = num2 * i;
                System.out.println("\nThe factorial of " + num + " is " + num2);
                System.out.println();
            }
        }
    }else if (num < 0) {
        System.out.println("Please input a valid integer. Program stopped.");
    }
}

将计算阶乘的代码放在一个单独的方法中,并在 main() 的循环中调用它。

import java.util.Scanner;

public class Loops {

    public static void printFactorial(int num) {
        int i = 0;
        int num2 = 0;
        if (num > 0) {
            System.out.print(num + "! = ");
            for (i = 1; i <= num; i++) {
                if(i < num){
                    System.out.print(i + " x ");
                    num2 = num2 * i;
                }
                if(i == num){
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of " + num + " is " + num2);
                    System.out.println();
                }
            }
        }else if (num < 0) {
            System.out.println("Please input a valid integer. Program stopped.");
            System.exit(0);
        }

    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int num = 0;
        System.out.println("-------------Factorial Calculator-------------");
        while ( true ) {
            System.out.print("Enter a positive integer: ");
            num = input.nextInt();
            printFactorial(num);
        }
    }
}
import java.util.Scanner;

public class Main {
public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    
    
    boolean runMore = true;
    
    while(runMore){
    
    

    int num, num2 = 1, x, i;

    System.out.println("-------------Factorial Calculator-------------");
    System.out.print("Enter a positive integer: ");
    num = input.nextInt();

    if (num > 0) {
        System.out.print(num + "! = ");
        for (i = 1; i <= num; i++) {
            if(i < num){
                System.out.print(i + " x ");
                num2 = num2 * i;
            }
            if(i == num){
                System.out.print(i);
                num2 = num2 * i;
                System.out.println("\nThe factorial of " + num + " is " + num2);
                System.out.println();
            }
        }
    }else if (num < 0) {
        System.out.println("Please input a valid integer. Program stopped.");
    }
    
    System.out.println("Want to contine : Enter '1' for yes and '0' for no");
    int wantToContinue = input.nextInt();
    
    if(wantToContinue==0){
        
        runMore = false;
    }
    
    }
}
}
    
    
import java.util.Scanner;

public class FactorialCalculator1 {

public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        
        int num;
        
        System.out.println("<------Factorial Calculator------>");
        while ( true ) {
            
        System.out.print("Enter a positive integer: ");
        num = input.nextInt();
        
        boolean runMore = true;
        
        int i;
        int num2 = 1;
        
        if (num > 0) {
            System.out.print(num + "! = ");
            for (i = 1; i <= num; i++) {
                if(i < num){
                    System.out.print(i + " x ");
                    num2 = num2 * i;
                }
                if(i == num){
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of " + num + " is: " + num2);
                    System.out.println();
                }
            }
        }else if (num < 0) {
            System.out.println("Invalid input! Program stopped!");
            System.exit(0);
           }
        }
    }
}