我拼命地试图让我的代码的最后一列将所有中间列的数字加起来。但它似乎已经从 1 开始了。
另外,一个附带问题:当我输入 12 时,我得到 11 个结果,因为“y”从 1 开始。有没有办法在循环中解决这个问题?
import javax.swing.JOptionPane;
public class Project
{
public static void main( String[] args )
{
String input = JOptionPane.showInputDialog( "How many Fibonacci numbers
do you wish to see?" +
"\n" + "Choose 1-50!");
int numFib = Integer.parseInt( input );
int numbers[] = new int[numFib];
int fibonacci[] = new int[50];
for( int index = 1; index < numbers.length; index++ )
{
numbers[index] = index;
}
for( int x = 0; x < numbers.length; x++ )
{
if( x == 0 )
{
fibonacci[0] = 0;
}
if( x == 1 )
{
fibonacci[1] = 1;
}
if( x > 1 )
{
fibonacci[x] = fibonacci[x-2] + fibonacci[x-1];
}
}
System.out.println( "Number" + "\t" + "Fibonacci Number" + "\t" +
"Running Total of FNs" );
for( int y = 1; y < numbers.length; y++ )
{
int total = fibonacci[y] + fibonacci[y - 1];
System.out.println( numbers[y] + "\t" + fibonacci[y - 1]
+ "\t" + "\t" + "\t" + total );
}
}
}