本文介绍了使用java .my代码的Euler数(数学常量)无法正常工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想写一个java代码,可以计算euler数e = 2.718281828459 ...), e = sigma(k = 0到n)1 / n! 命令设置上面公式中的数字n。 我有两个问题: 1.我认为我的euler方法不正确。 2.我应该如何打印我的代码,将近似数字四舍五入到最接近的四位(或其他小数位)。 什么我试过了: < pre> package edu.kit.informatik; import java.util.Scanner; public class Main { // 主要方法 public static void main( String [] args){ System.out.println( 键入数字); 扫描仪输入= 新扫描仪(System.in); int n = input.nextInt(); System.out.println(euler(n)); } // factorial Method public static int factorial( int n){ for ( int i = 0 ; i< = n; i ++){ if ( n == 0 ) return 1 ; else return n * factorial(n - 1 ); } return factorial(n); } // Euler public static double euler( int n){ double sum = 0 。 0 ; for ( int i = 0 ; i< = n; i ++){ sum + = 1 / factorial(i); return sum; } return euler(n); } } 解决方案 sum + = 1 / factorial(i); 小心,关于Java中2个整数的除法,它会进行整数除法! 从Java开始 - 第2单元数学运算符 [ ^ ] Quote: 1。我认为我的euler方法并不正确。 别想!确保! 你的代码没有你想象的那样,你不明白为什么! 有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。 调试器在这里向您展示您的代码正在做什么,您的任务是与它进行比较应该这样做。 调试器中没有魔法,它不知道你应该做什么,它没有找到错误,只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。 要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。 调试器 - 维基百科,免费的百科全书 [ ^ ] http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ] https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ] 调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。 1。为什么? 2.使用 String(Java Platform SE 7) [ ^ ] I want to write a java code, which could count euler number e = 2.718281828459...) ,e= sigma(k=0 to n) 1/n!A command sets the number n in the formula above.I have two problems :1. I think my euler method is not corrct .2. how should I wirte my code to print the approximated number rounded to the nearest four (or what else) decimal places.What I have tried:<pre>package edu.kit.informatik;import java.util.Scanner;public class Main { // Main Method public static void main(String[] args) { System.out.println("type a number"); Scanner input = new Scanner(System.in); int n = input.nextInt(); System.out.println(euler(n)); } // factorial Method public static int factorial(int n) { for (int i = 0; i <= n; i++) { if (n == 0) return 1; else return n * factorial(n - 1); } return factorial(n); } // Euler public static double euler(int n) { double sum = 0.0; for (int i = 0; i <= n; i++) { sum += 1 / factorial(i); return sum; } return euler(n); }} 解决方案 sum += 1 / factorial(i);Be careful, about division of 2 integers in Java, it makes an integer division !Beginning Java - Unit 2 Math Operators[^]Quote:1. I think my euler method is not corrct .Do not think ! Ensure !Your code do not behave the way you expect, and 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 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[^]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.1. Why?2. Use a string format as described at String (Java Platform SE 7 )[^] 这篇关于使用java .my代码的Euler数(数学常量)无法正常工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 21:12