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

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

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

问题

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

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

我认为在整个程序中我没有除以零的情况!
莱布尼茨公式:
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:

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    //in order to celebrate pi day I have made this program that calculates pi with the Leibniz Formula
     System.out.println(&quot;enter the number of iterations you want:&quot;);
    double EstimatedPi=0;
    Scanner input= new Scanner(System.in);
    double iterations=input.nextDouble();
    double denominator = 1;
    for(int i=0;0&lt;iterations;i++){
      if(i%2 == 0){
        EstimatedPi=EstimatedPi + (1/denominator);
      }
      else{
        EstimatedPi=EstimatedPi-(1/denominator);
      }
      denominator=denominator+2;
      EstimatedPi=EstimatedPi*4;
      System.out.println(EstimatedPi);
    }
  }
}

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.

public class Main {

    public static void main(String[] args) {// in order to celebrate pi day I have made this program that calculates pi
                                            // with the Leibniz Formula
        System.out.println("enter the number of iterations you want:");
        double EstimatedPi = 0;
        Scanner input = new Scanner(System.in);
        double iterations = input.nextDouble();
        double denominator = 1;
        for (int i = 0; i < iterations; i++) {
            if (i % 2 == 0) {
                EstimatedPi = EstimatedPi + (1 / denominator);
            } else {
                EstimatedPi = EstimatedPi - (1 / denominator);
            }
            denominator = denominator + 2;
            EstimatedPi = EstimatedPi * 4;
            System.out.println(EstimatedPi);
        }
    }
}
英文:

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.

public class Main {

	public static void main(String[] args) {// in order to celebrate pi day I have made this program that calculates pi
											// with the Leibniz Formula
		System.out.println(&quot;enter the number of iterations you want:&quot;);
		double EstimatedPi = 0;
		Scanner input = new Scanner(System.in);
		double iterations = input.nextDouble();
		double denominator = 1;
		for (int i = 0; i &lt; iterations; i++) {
			if (i % 2 == 0) {
				EstimatedPi = EstimatedPi + (1 / denominator);
			} else {
				EstimatedPi = EstimatedPi - (1 / denominator);
			}
			denominator = denominator + 2;
			EstimatedPi = EstimatedPi * 4;
			System.out.println(EstimatedPi);
		}
	}
}

答案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:

确定