我的 Java 关于带阶乘的循环的代码不能正常工作
My Java code that is about for loop with factorial doesnt work like it should
我的 Java 代码应该让用户输入一个数字,然后计算该数字的阶乘,我需要使用 "for loop" 当我输入数字 5 时,它告诉我阶乘是 6它应该是 120。我曾尝试观看带有因式循环的教程,但它们不起作用,我认为这是因为我有 "do" 从调用
获取值的命令
代码如下:
static Scanner kboard = new Scanner(System.in); //variable to read in values
public static void main(String[] args) {
int choice = 0;
String dummy = "";
String forename = "";
String surname = "";
int number = 0;
do {
System.out.println("1. display the user name, 2. calculate factorial, 3. exit");
choice = kboard.nextInt();
dummy = kboard.nextLine(); //strips out the return
if (choice == 1) {
forename = getforename();
surname = getsurname();
displaydetails(forename, surname);
}
if (choice == 2) {
number = getnumber();
calcfactorial(number);
}
if (choice == 3) {
System.out.println("bye");
}
} while (choice != 3);
}
public static String getforename() {
String newforename = "";
System.out.println("Please enter your forename ?");
newforename = kboard.next();
return (newforename);
} // end getforename
public static String getsurname() {
/*
Code to prompt the user to enter a surname
*/
String newsurname = "";
System.out.println("Please enter your surname ?");
newsurname = kboard.next();
return (newsurname);
} // end getsurname
public static void displaydetails(String displayforename, String displaysurname) {
/*
Code will carry out prescribed changes and display the result
*/
char displayinitial;
String displayusername = "";
displaysurname = displaysurname.toUpperCase();
displayinitial = displayforename.charAt(0);
displayusername = displayinitial + displaysurname;
System.out.println("Your username is " + displayusername);
}
public static int getnumber() {
System.out.println("What numbers factorial do you want to know?");
int newnumber = kboard.nextInt();
return newnumber;
}
public static void calcfactorial(int newnumber) {
int count = 0;
int factorial = 1;
if (newnumber > 0) {
for (count = 1; count <= newnumber; count++); {
factorial = factorial * count;
System.out.println("Factorial of " + newnumber + " is: " + factorial);
}
} else {
System.out.println("Number must be positive");
}
}
如果您使用了调试器,那么您可以看出它只在 calcfactorial
方法中执行一次乘法运算,此时 count
已经是 6
。原因:
首先,删除 for
条件循环末尾的分号。它充当 for
循环的主体。这使得 count
等于 newnumber + 1
,或 6
.
其次,在 for
循环结束后移动打印语句,但仍在 if
块内。否则你会得到 newnumber
行打印输出。
我不会完全给出答案...用户 azurefrog 发布了一篇关于调试代码的有用文章link。
但是,您的 for 循环正在做一些您不希望做的事情:
public static void calcfactorial(int newnumber)
{
int count = 0;
int factorial = 1;
if (newnumber > 0)
{
for (count=1;count<=newnumber;count++);
{
factorial = factorial * count; System.out.println("Factorial of "+newnumber+" is: "+factorial);
}
}
else
{
System.out.println("Number must be positive");
}
}
通读一遍,factorial = factorial * count;
行将执行 1*1、1*2、1*3、1*4、1*5 等,无论输入什么都要计算对于阶乘。这不是正确的逻辑。
我建议更仔细地思考 for
循环的逻辑。您应该在该循环的某处使用输入的数字(即您给出的示例中的 5)。
我的 Java 代码应该让用户输入一个数字,然后计算该数字的阶乘,我需要使用 "for loop" 当我输入数字 5 时,它告诉我阶乘是 6它应该是 120。我曾尝试观看带有因式循环的教程,但它们不起作用,我认为这是因为我有 "do" 从调用
获取值的命令代码如下:
static Scanner kboard = new Scanner(System.in); //variable to read in values
public static void main(String[] args) {
int choice = 0;
String dummy = "";
String forename = "";
String surname = "";
int number = 0;
do {
System.out.println("1. display the user name, 2. calculate factorial, 3. exit");
choice = kboard.nextInt();
dummy = kboard.nextLine(); //strips out the return
if (choice == 1) {
forename = getforename();
surname = getsurname();
displaydetails(forename, surname);
}
if (choice == 2) {
number = getnumber();
calcfactorial(number);
}
if (choice == 3) {
System.out.println("bye");
}
} while (choice != 3);
}
public static String getforename() {
String newforename = "";
System.out.println("Please enter your forename ?");
newforename = kboard.next();
return (newforename);
} // end getforename
public static String getsurname() {
/*
Code to prompt the user to enter a surname
*/
String newsurname = "";
System.out.println("Please enter your surname ?");
newsurname = kboard.next();
return (newsurname);
} // end getsurname
public static void displaydetails(String displayforename, String displaysurname) {
/*
Code will carry out prescribed changes and display the result
*/
char displayinitial;
String displayusername = "";
displaysurname = displaysurname.toUpperCase();
displayinitial = displayforename.charAt(0);
displayusername = displayinitial + displaysurname;
System.out.println("Your username is " + displayusername);
}
public static int getnumber() {
System.out.println("What numbers factorial do you want to know?");
int newnumber = kboard.nextInt();
return newnumber;
}
public static void calcfactorial(int newnumber) {
int count = 0;
int factorial = 1;
if (newnumber > 0) {
for (count = 1; count <= newnumber; count++); {
factorial = factorial * count;
System.out.println("Factorial of " + newnumber + " is: " + factorial);
}
} else {
System.out.println("Number must be positive");
}
}
如果您使用了调试器,那么您可以看出它只在 calcfactorial
方法中执行一次乘法运算,此时 count
已经是 6
。原因:
首先,删除 for
条件循环末尾的分号。它充当 for
循环的主体。这使得 count
等于 newnumber + 1
,或 6
.
其次,在 for
循环结束后移动打印语句,但仍在 if
块内。否则你会得到 newnumber
行打印输出。
我不会完全给出答案...用户 azurefrog 发布了一篇关于调试代码的有用文章link。
但是,您的 for 循环正在做一些您不希望做的事情:
public static void calcfactorial(int newnumber)
{
int count = 0;
int factorial = 1;
if (newnumber > 0)
{
for (count=1;count<=newnumber;count++);
{
factorial = factorial * count; System.out.println("Factorial of "+newnumber+" is: "+factorial);
}
}
else
{
System.out.println("Number must be positive");
}
}
通读一遍,factorial = factorial * count;
行将执行 1*1、1*2、1*3、1*4、1*5 等,无论输入什么都要计算对于阶乘。这不是正确的逻辑。
我建议更仔细地思考 for
循环的逻辑。您应该在该循环的某处使用输入的数字(即您给出的示例中的 5)。