如何在java中创建阶乘? [英] How do I create a factorial in java?

查看:88
本文介绍了如何在java中创建阶乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中,我一直致力于创建一个阶乘。我不明白我的逻辑出错了。你能吗?



我尝试过的事情:



公共阶层阶乘扩展ConsoleProgram

{

int num = readInt(你想用什么数字计算阶乘?);



private static final int Max = 100;

private static final int Min = 0;



public void run()

{

int sum = 1;

for(int i = num; i = -1; i--)

{



sum = i *(i-1);



}

System.out.println(sum);

}



}

In codehs I have been working to create a factorial. I don't understand where my logic is going wrong. Can you?

What I have tried:

public class Factorial extends ConsoleProgram
{
int num = readInt ("What number would you like to compute the factorial for?");

private static final int Max = 100;
private static final int Min = 0;

public void run()
{
int sum = 1;
for(int i = num; i=-1; i--)
{

sum = i*(i-1);

}
System.out.println(sum);
}

}

推荐答案

for(int i = num; i=-1; i--)



您的测试条件永远不会失败。它只是将i设置为值-1,这将始终为真。它应该(可能)是:


Your test condition will never fail. It just sets i to the value -1, which will always be true. It should (probably) be:

for(int i = num; i != -1; i--)



所以循环只要我等于-1,就会继续。当它达到该值时,循环将终止。


So the loop continues as long as i is not equal to -1. When it reaches that value the loop will terminate.


引用:

我不明白我的位置逻辑错了。

I don't understand where my logic is going wrong.



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较做。

调试器中没有魔法,它不知道你的cpde应该做什么,它没有找到bug,只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

http://docs.oracle.com/javase/7/docs /technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里只显示你的内容你的代码正在做,你的任务是与它应该做的事情进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何在java中创建阶乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆