在java中使用do-while循环的斐波那契数列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19310786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fibonacci sequence using a do-while loop in java?
提问by Ryan
I am trying to print a Fibonaccisequence using a do-while
loop in java and can't understand this. Needs to be between 0 and 100.
我正在尝试使用java 中的循环打印斐波那契数列do-while
,但无法理解这一点。需要在 0 到 100 之间。
I have the following code:
我有以下代码:
int prevPrevVal = 0;
int prevVal = 1;
int currVal;
System.out.println(prevPrevVal);
System.out.println(prevVal);
do
{
currVal = prevVal + prevPrevVal;
System.out.println(currVal);
prevPrevVal = prevVal;
prevVal = currVal;
} while (prevVal <= 100);
回答by NewUser
This should be your solution
这应该是您的解决方案
public static void main(String[] args) {
int prevVal = 1;
int prevPrevVal = 0;
int n = 0;
do{
int currVal = prevVal + prevPrevVal;
prevPrevVal = prevVal;
prevVal = currVal;
System.out.print(currVal+" ");
n++;
}while(n<5);//n is the number of terms
}
回答by Vimal Bera
Here you go :
干得好 :
int prevVal = 1;
int prevPrevVal = 0;
do{
int currVal = prevVal + prevPrevVal;
//currVal is your Fibonacc seq.
prevPrevVal = prevVal;
prevVal = currVal;
}
while(yourCondition);
回答by LOL. NO.
Using the basic structure of a do-while
loop from the documentation:
使用文档中do-while
循环的基本结构:
do {
statement(s)
} while (expression);
What you want in the "statement(s)" section is to increment (and possibly output) your result through each iteration. A basic Fibonacci sequence using a do-while
loop would look like the following:
您在“语句”部分想要的是通过每次迭代增加(并可能输出)您的结果。使用do-while
循环的基本斐波那契数列如下所示:
int prevVal = 1;
int prevPrevVal = 0;
int currVal;
do {
// Add the two numbers
currVal = prevVal + prevPrevVal;
// "Bump" up prevPrevVal to prevVal, and prevVal to currVal
prevPrevVal = prevVal;
prevVal = currVal;
// Output to the screen
System.out.println(currval + "\n");
} while(expression);
回答by aakansha
Since you want the terms of Fibonacci up to 100,just change while (prevVal <= 100);
to while (prevVal+prevPrevVal <= 100);
由于您希望斐波那契数达到 100,只需更改while (prevVal <= 100);
为while (prevVal+prevPrevVal <= 100);
This will print up to 89.
这将最多打印 89 个。
回答by Sasi
This is a simplified program to find out the Fibonacci series by providing the conditional limit in the while loop. Hope you guys get an idea over with this....!!
这是一个通过在 while 循环中提供条件限制来找出斐波那契数列的简化程序。希望你们对此有一个想法......!
int a=0;
int b=0;
int temp=1;
do {
a=b;
b=temp;
temp=a+b;
System.out.println(temp);
}while(temp<100);
回答by Dantelog
Managed to do it with two variables and without printing a number out of range. Sorry if it's crappy, it's my first day of programming :)
设法使用两个变量来完成它,并且没有打印超出范围的数字。对不起,如果它很糟糕,这是我编程的第一天:)
int x = 0;
int y = 1;
do
{
System.out.println(x);
y = x + y;
if (y < 100)
{
System.out.println(y);
}
x = x + y;
} while (x < 100);