Java控制台无限次打印出“infinity”,而不是值?

huangapple go评论105阅读模式
英文:

Java console printing out "infinity" infinite times instead of value?

问题

我正试图制作一个程序,使用莱布尼茨公式估计圆周率,作为庆祝圆周率日的一部分。然而,与其打印出我想要的估计圆周率值,控制台却打印出了“无穷大”,一直打印直到我终止执行。我非常困惑,需要帮助!
代码:

  1. import java.util.Scanner;
  2. class Main {
  3. public static void main(String[] args) {
  4. //为了庆祝圆周率日,我制作了这个使用莱布尼茨公式计算圆周率的程序
  5. System.out.println("请输入您想要的迭代次数:");
  6. double EstimatedPi = 0;
  7. Scanner input = new Scanner(System.in);
  8. double iterations = input.nextDouble();
  9. double denominator = 1;
  10. for(int i = 0; i < iterations; i++){
  11. if(i % 2 == 0){
  12. EstimatedPi = EstimatedPi + (1 / denominator);
  13. }
  14. else{
  15. EstimatedPi = EstimatedPi - (1 / denominator);
  16. }
  17. denominator = denominator + 2;
  18. EstimatedPi = EstimatedPi * 4;
  19. System.out.println(EstimatedPi);
  20. }
  21. }
  22. }

我认为在整个程序中我没有除以零的情况!
莱布尼茨公式:
https://en.wikipedia.org/wiki/Leibniz_formula_for
控制台截图:
screenshot
我的repl项目链接:
https://repl.it/@HarryPengRHS/Determining-the-value-of-pi-pi-day-attribute

英文:

I am trying to make a program that estimates pi with the Leibniz equation as an attribute to pi day, however instead of printing out the estimated pi value that I wanted the console is printing out "infinity" for infinite times until I terminate the execution. I am very very confused, help needed!
code:

  1. import java.util.Scanner;
  2. class Main {
  3. public static void main(String[] args) {
  4. //in order to celebrate pi day I have made this program that calculates pi with the Leibniz Formula
  5. System.out.println(&quot;enter the number of iterations you want:&quot;);
  6. double EstimatedPi=0;
  7. Scanner input= new Scanner(System.in);
  8. double iterations=input.nextDouble();
  9. double denominator = 1;
  10. for(int i=0;0&lt;iterations;i++){
  11. if(i%2 == 0){
  12. EstimatedPi=EstimatedPi + (1/denominator);
  13. }
  14. else{
  15. EstimatedPi=EstimatedPi-(1/denominator);
  16. }
  17. denominator=denominator+2;
  18. EstimatedPi=EstimatedPi*4;
  19. System.out.println(EstimatedPi);
  20. }
  21. }
  22. }

I don't think I divided anything by zero in the entire program!
the Leibniz formula:
https://en.wikipedia.org/wiki/Leibniz_formula_for
console screenshot:
screenshot
link to my repl project:
https://repl.it/@HarryPengRHS/Determining-the-value-of-pi-pi-day-attribute

答案1

得分: 1

Your for loop condition is wrong it should be for (int i = 0; i < iterations; i++)
you were using for(int i=0; 0 < iterations; i++){
0 < iterations which is always true and loop goes to infinite.

  1. public class Main {
  2. public static void main(String[] args) {// in order to celebrate pi day I have made this program that calculates pi
  3. // with the Leibniz Formula
  4. System.out.println("enter the number of iterations you want:");
  5. double EstimatedPi = 0;
  6. Scanner input = new Scanner(System.in);
  7. double iterations = input.nextDouble();
  8. double denominator = 1;
  9. for (int i = 0; i < iterations; i++) {
  10. if (i % 2 == 0) {
  11. EstimatedPi = EstimatedPi + (1 / denominator);
  12. } else {
  13. EstimatedPi = EstimatedPi - (1 / denominator);
  14. }
  15. denominator = denominator + 2;
  16. EstimatedPi = EstimatedPi * 4;
  17. System.out.println(EstimatedPi);
  18. }
  19. }
  20. }
英文:

Your for loop condition is wrong it should be for (int i = 0; i &lt; iterations; i++)
you were using for(int i=0;0&lt;iterations;i++){
0&lt;iterations which is always true and loop goes to infinite.

  1. public class Main {
  2. public static void main(String[] args) {// in order to celebrate pi day I have made this program that calculates pi
  3. // with the Leibniz Formula
  4. System.out.println(&quot;enter the number of iterations you want:&quot;);
  5. double EstimatedPi = 0;
  6. Scanner input = new Scanner(System.in);
  7. double iterations = input.nextDouble();
  8. double denominator = 1;
  9. for (int i = 0; i &lt; iterations; i++) {
  10. if (i % 2 == 0) {
  11. EstimatedPi = EstimatedPi + (1 / denominator);
  12. } else {
  13. EstimatedPi = EstimatedPi - (1 / denominator);
  14. }
  15. denominator = denominator + 2;
  16. EstimatedPi = EstimatedPi * 4;
  17. System.out.println(EstimatedPi);
  18. }
  19. }
  20. }

答案2

得分: 0

我遇到了同样的问题(控制台打印“-Infinity”)。

对我来说,我正在尝试对一个空数组取最大值

Math.max(...[]) 打印出 -Infinity

希望这能帮助其他遇到这个问题的人。

英文:

I ran into this same issue (console printing "-Infinity").

For me, I was trying to take the maximum of an empty array

Math.max(...[]) prints -Infinity

Hope this helps somebody else that runs into this problem

huangapple
  • 本文由 发表于 2020年3月15日 11:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/60689537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定