gpt4 book ai didi

Java for循环获取X

转载 作者:行者123 更新时间:2023-12-01 13:50:13 25 4
gpt4 key购买 nike

我需要有关作业的 for 循环的帮助。有一道数学题叫N!,我敢打赌你们有些人听说过它。它就像 1*2*3*4*5*n=x我做了一个这样的表:
1 = 1
1 * 2 = 2
1 * 2 * 3 = 6
1 * 2 * 3 * 4 = 24
1 * 2 * 3 * 4 * 5 = 120
1 * 2 * 3 * 4 * 5 * 6 = 720
但我似乎无法解决这个问题。如何从 1*2*3*4....*n=x 中得到 x 的值?到目前为止,这是我的代码:

Scanner input = new Scanner(System.in);
System.out.println("\n~~Assignment 8.5~~");
boolean go = true;

do {
int n;
int total;
System.out.println("Loop until:");
n = input.nextInt();
for (int i = 1;i <= n;i++)
{

System.out.print(i);
if (i == n) { System.out.print(" = " + "idk" + "\n"); break;} else { System.out.print(" * ");}
}
} while ( go == true);

最佳答案

只需添加总计计算即可在 for 循环内:

total *= i;

并在for循环之后打印它。请记住使用值 1

初始化总计

关于Java for循环获取X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20020762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com