8 Replies - 719 Views - Last Post: 20 March 2014 - 03:18 PM Rate Topic: -----

#1 LooneyTunerIan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-March 14

A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:08 PM

I got a problem with my Fibonacci Sequence Code:

public class FibSeqByIanNeumann {

    public static void main(String[] args) 
    {
        
    
        Scanner input = new Scanner(System.in); 
        int fib, count;

        System.out.print("Please enter number: ");
        count = input.nextInt();
            
        fib = 0;

        for (int i=2; i<=count; i++)
        {
            
            fib = count + fib;
            
        }
            
            System.out.print("When a Fibonacci Sequence runs " + count + " times, it goes like this: " + "\n");
            System.out.println(fib + ", ");
        
    }
}


It's not running the way it's supposed to. Can anyone help fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: A problem with my Fibonacci Sequence code

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:10 PM

The last time I checked the Fibonacci Sequence was the previous number plus the current number where the 0 and 1 indexed terms are 0 and 1.

Your for loop does not appear to be doing that..
Was This Post Helpful? 0
  • +
  • -

#3 LooneyTunerIan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-March 14

Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:11 PM

Can anyone show me it's supposed to work?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:18 PM

I would suggest starting with a few variables..

Have three variables..

temp, current, previous.

Copy current to temp.
Set current to be current plus previous.
Set previous to temp.
Repeat.
Was This Post Helpful? 0
  • +
  • -

#5 LooneyTunerIan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-March 14

Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:18 PM

I'm confused. :(
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: A problem with my Fibonacci Sequence code

Posted 19 March 2014 - 05:21 PM

I literally wrote it out for you. Outside of just giving your the code - how about you try matching the code to what I typed out.
Was This Post Helpful? 0
  • +
  • -

#7 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: A problem with my Fibonacci Sequence code

Posted 20 March 2014 - 11:56 AM

i belive ive seen code for this in this forum from the past, try doing a search
Was This Post Helpful? 0
  • +
  • -

#8 LooneyTunerIan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-March 14

Re: A problem with my Fibonacci Sequence code

Posted 20 March 2014 - 03:09 PM

All I need is a java code that only involve an easy for loop and can easily start at zero.

So far, none of the scripts I've looked up contains those things.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: A problem with my Fibonacci Sequence code

Posted 20 March 2014 - 03:18 PM

Again - I wrote out the sequence of steps.. you should be able to at least try to put code to it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1