阶乘计算器

问题描述 投票:0回答:6

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

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.");
        }
    }
}
java loops factorial
6个回答
1
投票

将计算阶乘的代码放在单独的方法中,并在 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);
        }
    }
}

1
投票

使用

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.");
    }
}

0
投票
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;
    }
    
    }
}
}
    
    

0
投票

当用户不输入正数时使用 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();
                }
            }
        }
    }
}

0
投票
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);
           }
        }
    }
}

0
投票
package condiloop.Functions;
import java.util.Scanner;

public class factorial {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a number to calculate its factorial: ");
        int num = scanner.nextInt();
        
        long result = Factorial(num);
        System.out.println("Factorial of " + num + " is " + result);
        
        scanner.close();
    }
    
    public static long Factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        } else {
            long fact = 1;
            for (int i = 2; i <= n; i++) {
                fact *= i;
            }
            return fact;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.