【发布时间】:2020-01-23 02:24:08
【问题描述】:
我想我必须使用 String goAgainResponse 来避免无限循环,但我不知道该代码会是什么样子。我应该通过添加 4 - 4/3 + 4/5 - 4/7 + 4/9 等来近似 pi >
int term;
int counter = 1;
double piCalc=0.0;
String goAgainResponse = null;
boolean goAgain = false;
Scanner kb = new Scanner(System.in);
/* NOTE you may not declare any additional variables */
do
{
System.out.println("This program will approximate Pi based on the following series:");
System.out.println("4 - 4/3 + 4/5 - 4/7 + 4/9 - ...");
System.out.println("Enter the term number to which you would like to approximate Pi");
term = kb.nextInt();
while(term <= 9)
{
System.out.print("Please enter a number greater than 9:");
term = kb.nextInt();
}
while(term > 9)
{
term += 1;
piCalc = 4/(2 * term - 1);
System.out.print(piCalc);
}
System.out.print("Would you like to try again (yes/no)? ");
}while(goAgain);
【问题讨论】:
标签: java loops while-loop infinite-loop do-while