Java 查找小于输入数字的最大阶乘

Java 查找小于输入数字的最大阶乘,java,numbers,factorial,Java,Numbers,Factorial,本质上,我想让我的代码做的是将阶乘结果与输入的数字进行比较,找到比输入的数字小的最大阶乘。出于某种原因,它没有打印任何东西 public class Main { public static void main(String[] args) { int numinput = 150; //number than we are trying to find the largest factorial less than int num = 1; //number than

本质上,我想让我的代码做的是将阶乘结果与输入的数字进行比较,找到比输入的数字小的最大阶乘。出于某种原因,它没有打印任何东西

public class Main {
  public static void main(String[] args) {
     int numinput = 150; //number than we are trying to find the largest factorial less than
     int num = 1; //number than we are solving a factorial for, to test against numinput
     int factorial = 1; //actual result of the factorial
     while (factorial < numinput) //finds the factorial of num
       for(int i = 1; i <= num; i++) {
         factorial *= i;
       }
     num++;
     if (factorial > numinput) {
        num--;
        System.out.println("The largest factorial less than " + numinput + "is !" + factorial);
     }
  }
}
公共类主{
公共静态void main(字符串[]args){
int numinput=150;//number than我们试图找到小于的最大阶乘
int num=1;//我们要解决的阶乘的数量,以测试numput
int factorial=1;//阶乘的实际结果
while(factorial
while
循环没有花括号,因此循环体中唯一的东西就是
for
循环
num
1
开始,循环中没有任何内容会增加它,因此它将永远循环

不过,您不需要嵌套循环-一个在运行时计算阶乘的循环就足够了:

int numinput = 150; //number than we are trying to find the largest factorial less than
int num = 1;
int factorial = 1;

while (factorial < numinput) {
    num++;
    factorial *= num;
}

// We overshot to terminate the loop, go back one number
factorial /= num;
num--;

System.out.println
    ("The largest factorial less than " + numinput + " is " + num + "!, or " + factorial);
int numinput=150//我们正试图找到比这个数小的最大阶乘
int num=1;
整数阶乘=1;
while(阶乘
while
循环没有花括号,因此循环体中唯一的东西就是
for
循环
num
1
开始,循环中没有任何内容会增加它,因此它将永远循环

不过,您不需要嵌套循环-一个在运行时计算阶乘的循环就足够了:

int numinput = 150; //number than we are trying to find the largest factorial less than
int num = 1;
int factorial = 1;

while (factorial < numinput) {
    num++;
    factorial *= num;
}

// We overshot to terminate the loop, go back one number
factorial /= num;
num--;

System.out.println
    ("The largest factorial less than " + numinput + " is " + num + "!, or " + factorial);
int numinput=150//我们正试图找到比这个数小的最大阶乘
int num=1;
整数阶乘=1;
while(阶乘
这是因为您的代码无法脱离此while循环-

while (factorial < numinput) //finds the factorial of num
       for(int i = 1; i <= num; i++) {
         factorial *= i;
       }
但是我检查了你的代码的输出150,它是不正确的。我在下面提供我的代码-

public class Main {
  public static void main(String[] args) {
     int numinput = 150; //number than we are trying to find the largest factorial less than
     int num = 1; //number than we are solivng a factorial for, to test agaisnt numinput
     int factorial = 1; //actual result of the factorial
     while (factorial <= numinput) {    // continue multiplying even if equal
        factorial = 1;
        for(int i = 1; i <= num; i++) {
            factorial *= i;
        }
        num++;
     }
    // now the factorial is surely greater than numinput, and it is the factorial of
    // current value of num - 1, we can remove the conditional
    // and reduce the factorial by num -1 since multiplying by num - 1 has
    // made it bigger than numinput
    factorial /= (num - 1);
    System.out.println("The largest factorial less than " + numinput + "is !" + factorial);
  }
}
公共类主{
公共静态void main(字符串[]args){
int numinput=150;//number than我们试图找到小于的最大阶乘
int num=1;//比我们要求解的阶乘的数字,要测试的不是numput
int factorial=1;//阶乘的实际结果

while(factorial这是因为您的代码无法脱离while循环-

while (factorial < numinput) //finds the factorial of num
       for(int i = 1; i <= num; i++) {
         factorial *= i;
       }
但是我检查了你的代码输出150,它是不正确的。我在下面提供我的代码-

public class Main {
  public static void main(String[] args) {
     int numinput = 150; //number than we are trying to find the largest factorial less than
     int num = 1; //number than we are solivng a factorial for, to test agaisnt numinput
     int factorial = 1; //actual result of the factorial
     while (factorial <= numinput) {    // continue multiplying even if equal
        factorial = 1;
        for(int i = 1; i <= num; i++) {
            factorial *= i;
        }
        num++;
     }
    // now the factorial is surely greater than numinput, and it is the factorial of
    // current value of num - 1, we can remove the conditional
    // and reduce the factorial by num -1 since multiplying by num - 1 has
    // made it bigger than numinput
    factorial /= (num - 1);
    System.out.println("The largest factorial less than " + numinput + "is !" + factorial);
  }
}
公共类主{
公共静态void main(字符串[]args){
int numinput=150;//number than我们试图找到小于的最大阶乘
int num=1;//比我们要求解的阶乘的数字,要测试的不是numput
int factorial=1;//阶乘的实际结果

而(factorial我假设您所说的是小于输入的factorial的最小整数factorial。因此,代码应该如下所示:


public static void main(String[] args) {
    int input = 150; // example 
    for (int i = 1; i <= input - 1; i++) {
    int sum = (input - 1) * 1;
    }

    System.out.println(input);
}

    

公共静态void main(字符串[]args){
int input=150;//示例

对于(int i=1;i我假设您所说的最小整数阶乘小于输入的阶乘。因此,代码应该如下所示:


public static void main(String[] args) {
    int input = 150; // example 
    for (int i = 1; i <= input - 1; i++) {
    int sum = (input - 1) * 1;
    }

    System.out.println(input);
}

    

公共静态void main(字符串[]args){
int input=150;//示例
对于(int i=1;i