java - Get running total of numbers? -
i desperately trying last column of code add middle column numbers in running total.. seems start @ 1 already.
also, side question: when input 12.. 11 results, due "y" starting @ 1. there anyway fix in loop?
import javax.swing.joptionpane; public class project { public static void main( string[] args ) { string input = joptionpane.showinputdialog( "how many fibonacci numbers wish 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 ); } } }
to start start y zero:
int total = 0; for( int y = 0; y < numbers.length; y++ ) { total += fibonacci[y]; system.out.println( numbers[y] + "\t" + fibonacci[y] + "\t" + "\t" + "\t" + total ); } total -= fibonacci[0] + fibonacci[numbers.length-1];
Comments
Post a Comment