7 Replies - 312 Views - Last Post: 10 January 2013 - 10:22 AM Rate Topic: -----

#1 jwhiteknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-January 13

fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 07:45 AM

I need help creating a loop.The loop will ask the user for a number in the Fibonacci series. if the user enter 3... the program will print out 2 in the Fibonacci series, the program will keep asking the user to enter a new number unless the user enters 0. I keep getting an error in the method line." class, interface, enum". If I take out the " do while loop" the program works. I am thinking that the loop is stopping the method call, but I am not sure how to stop that.

Scanner i = new Scanner (System.in);

System.out.println("Given a number n, this program will return the N-th Fibonacci number in an interger type.\n");

do
{

int number = 0;

System.out.print ("Please input your number: ");
number = i.nextInt();

System.out.printf (" The %d-th is fibinocci number is %.0f\n",number, fibcalic(number));

}
while (number != 0);

}

} // end main method

public static int fibcalic(int number)// method
{
int max = number;
int[] fibarray = new int [max];// set the array to int max is still int

fibarray [0] = 0;
fibarray [1] = 1;

for (int a=2; a < max; a++)
{
fibarray[a] = fibarray[a -1] + fibarray[a -2];
}
return fibarray[number];
}


This post has been edited by jon.kiparsky: 10 January 2013 - 07:52 AM
Reason for edit:: fixed [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: fibonacci loop repeadly ask the user a question unless user enter "

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,121
  • Joined: 19-March 11

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 08:01 AM

Since you've declared the variable "number" within the loop, it's only in scope within that loop. That means you can't refer to it after the loop ends.

Move this line
int number = 0;


so it's before the loop, and you should be okay.
Was This Post Helpful? 1
  • +
  • -

#3 jwhiteknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-January 13

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 09:36 AM

i moved the " int number = 0" out of the loop, but I still get this error
in line 22 " class, interface, or enum expect"
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,121
  • Joined: 19-March 11

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 09:46 AM

That usually indicates that your braces aren't matched up. Try indenting your code in the normal manner and your error should pop right out at you.
Normal indentation style looks like this:

public class Foo
{
  Thing bar;
 
  public Foo(Thing bar)
  {
    this.bar = bar;
  }

  public static void main(String args)
  {
    Foo foo = new Foo(new Thing());
    foo.bar.frobnicateWildlyUntilDone();
  }

}


Notice how every closing brace is lined up with its opening brace: there's no way I can have an extra right brace without seeing it. If I don't indent, I can't tell if it's right without counting.
Was This Post Helpful? 0
  • +
  • -

#5 jwhiteknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-January 13

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 10:12 AM

thank you that help. the program is running now, but I am getting an other error in line 13 and 34. " arrayindexoutofboundseception"

i don't think i have the output formatted correctly.
Was This Post Helpful? 0
  • +
  • -

#6 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 10:18 AM

int max = number;


Remember that arrays range from 0 to n-1.
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,121
  • Joined: 19-March 11

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 10:19 AM

If you want help with that, please post your revised source code and the actual error output that you're getting from the compiler.
Was This Post Helpful? 0
  • +
  • -

#8 jwhiteknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 09-January 13

Re: fibonacci loop repeadly ask the user a question unless user enter "

Posted 10 January 2013 - 10:22 AM

i was wrong the error is setting up the indexing for the array.
" int max = number; " get me an error message
but if i do
" into max = 71" a limit in the length of the array the program work
just fine

thank you for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1