Discuss / Java / 练习三这样应该更简洁

练习三这样应该更简洁

Topic source

弹痕0707

#1 Created at ... [Delete] [Delete and Lock User]

public class Main {

public static void main(String[] args) {

double pi = 0;

for (int i = 1; i < 10000; i += 2) {

pi+=4/(double)i;

pi=-pi;

}

System.out.println(Math.abs(pi));

}

}

弹痕0707

#2 Created at ... [Delete] [Delete and Lock User]

for (double i = 1; i < 10000; i += 2) {

pi=4/i-pi;

}

System.out.println(Math.abs(pi));

只要一行代码

赵棠棠

#3 Created at ... [Delete] [Delete and Lock User]

pi = 4.0 / i - pi;

不然结果会是3.0


  • 1

Reply