如何计算 java 中带循环的阶乘
How to calculate an factorial with loops in java
我正在尝试编写一个使用循环计算阶乘的程序的一部分。我没有任何错误消息,但我没有得到任何输出。
是否有针对我的方法的建议,或者是否有使用循环的更好方法?谢谢!
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
//Execute factorial
do{
factTotal = factTotal * number;
number--;
while (number >= 1);
}
while (number <= 0);{
System.out.println("That's not a positive integer!");
}
System.out.print(factTotal);
}
}
在您的代码中,在 do...while
循环中,您使用了这个 while
语句:
while (number >= 1);
你没注意到分号吗?这个循环的意义何在?目前它导致无限循环,因为 number
的值永远不会改变,因此你没有得到任何输出。
此外,您的第二个 while
循环应该更像是一个 if
语句,如下所示
那段代码相当于:
while (number >= 1)
{
//doNothing
}
我相信你的意思是:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
//Execute factorial
do{
factTotal = factTotal * number;
number--;
}
while (number <= 1);
if(number < 0)
{
System.out.println("That is not a positive integer");
}
else
System.out.print(factTotal);
}
这就是我处理问题的阶乘部分的方法。我会取消 do/while 循环,因为如果你没有得到输出,你就会陷入无限循环。
//Call this method when you want to calculate the factorial
public int factorial(int num){
for(int i = num-1; i > 1; i--){
num *= i;
}
return num;
}
这就是它在您的代码中的样子。
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
if(number > 0){
factTotal = factorial(number);
System.out.print(factTotal);
}
else
System.out.println("This is a negative number");
}
@Pernicious,请查看您的程序并稍加修改并添加我的评论。我想这就是你想要做的。
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
// The input number check should be before factorial calculation
if(number <= 0){
System.out.println("That's not a positive integer!");
System.exit(0);
}
//Execute factorial
do {
factTotal = factTotal * number;
number--;
// while (number >= 1); This while should be after do{}, not within do{}
} while (number >= 1);
// This check should be done immeduately after user input, not after calculation of factorial.
// while (number <= 0);
// {
// System.out.println("That's not a positive integer!");
// }
System.out.println(factTotal);
}
}
我正在尝试编写一个使用循环计算阶乘的程序的一部分。我没有任何错误消息,但我没有得到任何输出。
是否有针对我的方法的建议,或者是否有使用循环的更好方法?谢谢!
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
//Execute factorial
do{
factTotal = factTotal * number;
number--;
while (number >= 1);
}
while (number <= 0);{
System.out.println("That's not a positive integer!");
}
System.out.print(factTotal);
}
}
在您的代码中,在 do...while
循环中,您使用了这个 while
语句:
while (number >= 1);
你没注意到分号吗?这个循环的意义何在?目前它导致无限循环,因为 number
的值永远不会改变,因此你没有得到任何输出。
此外,您的第二个 while
循环应该更像是一个 if
语句,如下所示
那段代码相当于:
while (number >= 1)
{
//doNothing
}
我相信你的意思是:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
//Execute factorial
do{
factTotal = factTotal * number;
number--;
}
while (number <= 1);
if(number < 0)
{
System.out.println("That is not a positive integer");
}
else
System.out.print(factTotal);
}
这就是我处理问题的阶乘部分的方法。我会取消 do/while 循环,因为如果你没有得到输出,你就会陷入无限循环。
//Call this method when you want to calculate the factorial
public int factorial(int num){
for(int i = num-1; i > 1; i--){
num *= i;
}
return num;
}
这就是它在您的代码中的样子。
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
if(number > 0){
factTotal = factorial(number);
System.out.print(factTotal);
}
else
System.out.println("This is a negative number");
}
@Pernicious,请查看您的程序并稍加修改并添加我的评论。我想这就是你想要做的。
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
// The input number check should be before factorial calculation
if(number <= 0){
System.out.println("That's not a positive integer!");
System.exit(0);
}
//Execute factorial
do {
factTotal = factTotal * number;
number--;
// while (number >= 1); This while should be after do{}, not within do{}
} while (number >= 1);
// This check should be done immeduately after user input, not after calculation of factorial.
// while (number <= 0);
// {
// System.out.println("That's not a positive integer!");
// }
System.out.println(factTotal);
}
}