可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

I can't figure out what I'm missing on my code. Any help will be greatly appreciated. I'm new in coding and I'm just doing some practice. Thank you!

import java.util.Scanner;

import java.lang.Math;

public class Factorial {

public static Scanner sc;

public Factorial() {

}

int factorial(int n) {

for (int j = n - 1; j > 0; j--) {

int factorial = factorial(0) * j;

}

return factorial(0);

}

public static void main(String[] args) {

System.out.print("Enter a number to find its factorial: ");

sc = new Scanner(System.in);

int myNumber = sc.nextInt();

Factorial myFactorial = new Factorial();

System.out.println(myFactorial.factorial(myNumber));

}

}

回答1:

You're missing the corner case (for 0):

int factorial(int n) {

if (n == 0) return 1;

for (int j = n - 1; j > 0; j--) {

int factorial = factorial(0) * j;

}

return factorial(0);

}

回答2:

In recursive mode : `

import java.util.Scanner;

import java.lang.Math;

class Factorial {

public static Scanner sc;

public static int factorial(int n) {

if(n==0)return 1;

return n*factorial(n-1);

}

public static void main(String[] args) {

System.out.print("Enter a number to find its factorial: ");

sc = new Scanner(System.in);

int myNumber = sc.nextInt();

//Factorial myFactorial = new Factorial();

System.out.println(factorial(myNumber));

}

}`

回答3:

For every recursive function, you must need to write a base case else it will end up doing recursion and at the end it will cause stack overflow error(not the name of this site, it is an error), which is what you are missing right there in your algorithm.

For finding factorial recursively

Mathematically

n! = n x (n - 1)! and the base case is 0! = 1

In Java

// somewhere in your class

int factorial(int n) {

// base case

if (n == 0) return 1;

// recursion

return n * factorial(n-1);

}

回答4:

Well.... regardless of what you provide on n, you always return factorial(0) which ends up creating an endless loop of calls to factorial so I guess your stack is getting hammered and getting an Stack Overflow error, right? I think that's where the error is.

回答5:

Replace this code in your code:

int factorial=1;

int factorial(int n) {

for (int j = 1; j <= n; j++) {

factorial = factorial * j;

}

return factorial;

}

java factorial_Java Factorial Program相关推荐

  1. 导入项目报错报错Error:java: Cannot run program “D:/jdk/jdk1.7.0_67/bin/java“

    1.问题: Error:java: Cannot run program "D:/jdk/jdk1.7.0_67/bin/java" (in directory "C:\ ...

  2. Error:java: Cannot run program E:/Java/jdk1.7.0_80/bin/java (in directory C:\Users\dell\.IntelliJ

    Error:java: Cannot run program "E:/Java/jdk1.7.0_80/bin/java" (in directory "C:\Users ...

  3. JAVA:实现Factorial阶乘算法(附完整源码)

    JAVA:实现Factorial阶乘算法 package com.thealgorithms.maths;public class Factorial {/* Driver Code */public ...

  4. JAVA:实现Factorial递归形式阶乘算法(附完整源码)

    JAVA:实现Factorial递归形式阶乘算法 package com.thealgorithms.maths;public class FactorialRecursion {/* Driver ...

  5. java factorial_Java Longs.factorial(int n)用法及代码示例

    Guava LongMath类的factorial(int n)方法返回前n个正整数的乘积,即n!. 用法: public static long factorial(int n) 参数:该方法仅接受 ...

  6. 电商小程序Java_小程序电商客户端Demo 系统:小程序电商客户端源码 开发语言:Java(System: small program e - 下载 - 搜珍网...

    文件名大小更新时间 小程序电商客户端Demo\MiniShopClient\.git\config3372018-05-20 小程序电商客户端Demo\MiniShopClient\.git\desc ...

  7. 找不到 tools.jar。请检查 C:\Program Files\Java\jre1.8.0_151 是否包含有效的 JDK 安装

    今天全新安装了windows11之后 安装flutter等环境之后运行项目 却发现报错 找不到 tools.jar.请检查 C:\Program Files\Java\jre1.8.0_151 是否包 ...

  8. 【已解决】Error: could not open `C:\Program Files\Java\jre1.8.0_121\lib\amd64\jvm.cfg‘

    我的原因: java升级时,会在环境变量的path路径中增加以下两条路径,与我们安装java的路径重合. 解决方法: 删掉环境变量中的这两条语句, 同时找到对应文件,删除即可. C:\ProgramD ...

  9. java VM argument_java之program arguments与VM arguments

    program arguments 中的值作为 args[] 的参数传入的,而 VM Arguments 是设置的虚拟机的属性. program arguments 是要传给你的应用程序的,它通过主函 ...

最新文章

  1. 扔掉源码,15张图带你彻底理解java AQS
  2. No_16_0303 Java基础学习第十一天
  3. 线性表实现一元多项式的表示及相加(C语言实现)【线性表】
  4. 华为平板安装python_极致安卓—Termux/Aid Learning安装宇宙最强VS Code
  5. MVC4.0网站发布和部署到IIS7.0上的方法
  6. 洛谷P2085ssl1411OJ1370-最小函数值【堆,贪心】
  7. IntelliJ IDEA:使用Google Guava生成equals,hashCode和toString
  8. woocommerce 分类到菜单_Woocommerce商店显示分类
  9. 杂谈---如果你是面试官,你希望你未来的同事具有哪些能力呢?
  10. 设备 esp32_「ESP 教程」ESP32 如何运行 TensorFlow 模型
  11. 速成pytorch学习——8天损失函数
  12. 向张小龙开炮!百度祭出智能小程序
  13. 你的目的是什么是谁指使你_魔家四将的师傅是谁?隐藏的高人,只配合太上老君的布局...
  14. 中医预约管理系统都需要哪些功能?
  15. 28.XAPP1052驱动详解-WinDriver DMA读写流程
  16. excel筛选,排序
  17. 【英语词组】恋恋不忘Day4-2
  18. 腾讯历届笔试题(1)
  19. html中一些特殊的符号该如何表示
  20. 2023年节假日JSON

热门文章

  1. Python实例1:温度转换问题
  2. struts2.1.8 StrutsPrepareAndExecuteFilter 关于静态资源处理问题。访问classPath下静态资源
  3. 共享的excel无法删除工作簿
  4. sharepoint页面嵌入_Part 1: 如何把Power BI 嵌入到sharepoint 网站
  5. 抖音运营规则讲解系列(1):不符合社区规范内容被封丨国仁网络
  6. 2021年中秋国庆假期安排表新鲜出炉!这些你一定要知道
  7. 财政部新政:购房首付款调至20%
  8. html5播放rtsp视频流
  9. Google 关健词
  10. go os.Args 学习