FUFYN (05-05-2013)
Today I'm sharing a project of my creation.
I hope you enjoy
Crsdits:Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pkg5.pkg11.pkg3; import java****.BufferedReader; import java****.InputStreamReader; import java****.Console; import java********Exception; public class Principal { private static int seg; /** Programa desenvlvido por wallyson Marcos * 06/04/2013 */ public static void main(String[] args) throws IOException { BufferedReader entrada; entrada = new BufferedReader(new InputStreamReader(System.in)); long Fn=0; long Fn1=1; long b=1; long ent; long cont = 0; int opc ; try{ System.out.println("||=========================Menu of project===============================||"); System.out.println("||Enter 1 to determine how far will the nth Fibonacci number. ||"); System.out.println("||Enter 2 to determine the count of the number nth. ||"); System.out.println("||==================================================================||"); opc = Integer.parseInt(entrada.readLine()); switch(opc) { case 1:{ System.out.println("Enter a number to determine how far will the nth Fibonacci number"); ent = Integer.parseInt(entrada.readLine()); System.out.println("Its sequel: "); for(int i=2;i<=1000;i++){ System.out.println(Fn+";"); b=Fn; Fn=Fn+Fn1; Fn1=b; if(Fn>ent){break; } } break; } case 2:{ System.out.println("Enter a number to determine the count of the number nth. : "); ent = Integer.parseInt(entrada.readLine()); System.out.println("Its sequel: "); for(int i=1;i<=ent;i++){ System.out.println(Fn+";"+" Contador: "+i); b=Fn; Fn=Fn+Fn1; Fn1=b; } } break; default: System.out.println("Enter the correct option!"); } } catch (Exception e){ System.out.println("Enter a correct value!"); } } }
@ForeverRed
FUFYN (05-05-2013)
6ixth (05-08-2013)
Why did you use all this bufferedReader and inputStream stuff? Why not just use a simple scanner?
I'll change my profile pic when i get around to it, I originally put it there when i was 13, sorry.
ecHarley (06-29-2013)
Really bad quality code (no offense).
The fact that you repeat code twice in your switch is enough to see that you should use a method. I suggest you learning some about recursion:
This implementation is much better than yours. But if you see the algorithm step by step, you will see it could be much more efficient because some calls would return the same number.Code:public class Fibonacci{ public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int opt; // menu with the 2 options here. if(opt == 1){ int ent = scanner.nextInt(); System.out.println(fib(ent)); }else{ System.out.println(fib(1000)); } } public static long fib(long n){ long ret=0; if(n==0 || n==1){ ret = pos; }else{ ret = fib(pos-2)+fib(pos-1); } return ret; } }
Imagine: 5
ret = fib(3) + fib(4)
Now fib(4) will return:
ret = fib(2) + fib(3) Yeah, fib(3) again.
Last edited by ecHarley; 06-29-2013 at 10:02 AM.
I did a simple one... you can modify it and make say how many numbers you want it to make.
class main {
/**
* @param args
*/
public static void main(String[] args) {
int x=1;
int anterior = 0;
int temp;
while (true)
{
System.out.println(x);
temp = x;
x= x+anterior;
anterior = temp;
if(x>30)
{break;}
}
}
}
One of the biggest things in programming is to be efficient and avoid redundancies. Keep that in mind for next time!