如何编码阶乘 [英] how to code a factorial

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

问题描述

我的问题不只是代码,而是编写阶乘程序背后的逻辑.我目前正在赫尔辛基大学接受MOOC培训,而我一直陷于这种锻炼中.随着课程进行新的练习,说明变得越来越模糊.我知道这可能不是问这个问题的地方,如果您必须标记或删除它,我知道.由于我没有时间或金钱来上大学,所以我想独自学习.这门课程没有时间限制,我也不会因此而获得成就证书,我只是想要知识.

my question is not so much about code as it is the logic behind writing a factorial program. I am currently taking a MOOC at the University of Helsinki and I have become stuck on this exercise. As the course moves on to new exercises the instructions have become more and more vague. I realize this probably isn't the place to ask this question and if you must tag it or remove it, I do understand. I am trying to learn this on my own as I do not have the time or money to actually attend a university. This course has no time constraint and I wont be receiving a certificate of achievement for it, I simply want the knowledge.

这些是该练习的说明

创建一个程序来计算数字n的阶乘.阶乘n!使用公式1 * 2 * 3 * ... * n计算.例如4! = 1 * 2 * 3 * 4 =24.另外,定义为0! = 1.

Create a program that calculates the factorial of the number n. The factorial n! is calculated using the formula 1*2*3*...*n. For example 4! = 1*2*3*4 = 24. Additionally, it is defined that 0! = 1.

    // i don't understand the example that 4!= 1*2*3*4 = 24 
    // or how 0! = 1 pertains to multiplying numbers in succession to find the
    // factorial of the user input number.
    // i understand that 0! = 1 simply delclares that 0 is not equal to 1 
    // and 4 is not equal to 24, however if the 4! = portion of this statement
    // is in reference to the user input number 4 that statement would not be
    // true as 1*2*3*4 does equal 24 and 4 would be the number of executions
    // of the block execution of the loop required to write the factorial 
    // program.

   // EDIT: okay so according to this http://en.wikipedia.org/wiki/Factorial
   // i am wrong about what is being done here as they are not declaring 
   // that 4 not equals 24 but yet that 4! is a way of correlating the non
   // negative numbers up to 4, but given that math is not my strong suit 
   // it is even more confusing to me as to what i should be doing.

示例输出:

输入数字:3 阶乘是6

Type a number: 3 Factorial is 6

输入数字:10 阶乘是3628800

Type a number: 10 Factorial is 3628800

我当前的代码尝试如下

     public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);



    System.out.println("Type a number:");
    int userIn = Integer.parseInt(reader.nextLine());
    int factorial = 1;
    int extra = 1;
    int sum = 0;

    while (factorial <= userIn) {
        factorial++;
        sum = factorial + userIn + extra;
        }
    System.out.println("The factorial is:"+sum);
    }
}

我不明白我想念的是什么,我从研究中知道,在现实世界中,您不会编写此代码,因为有些库可以下载以执行阶乘功能,其效率比我能做到的要高得多代码,但是我不想因为其他人已经进行了编码并创建了一个图书馆来使我们的生活更轻松而跳过了本练习,所以我想学习本课程所提供的一切.如果我犯了一个简单的错误,那么我不介意提供所提供的代码更正,但是我想了解是什么使阶乘运算产生变化,可以这么说,而不仅仅是给出答案,这样我就可以继续.

I do not understand what it is that i am missing, i know from research that in the real world you would not code this as there are libraries you can download to perform the factorial function that are much more efficient than what i could code, but i don't want to simply skip this exercise with the knowledge that someone else has already coded and created a library to make our lives easier, i want to learn everything that this course has to offer. if i have made a simple error i don't mind an offered code correction, however i want to understand what makes the factorial operation tick so to speak, not just be given the answer so i can move on.

推荐答案

如果您不想使用外部函数,请尝试此

Try this one if you don't want to use an external function

public static void main(String[] args) {
   Scanner reader = new Scanner(System.in);
   System.out.println("Type a number:");
   int userIn = Integer.parseInt(reader.nextLine());
   int factorial = 1;
   int i= userin;
   while (userin >= 1) {
    factorial *= userIn;
    userin--;
   }
  System.out.println("The factorial is:"+factorial);
 }
}

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

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