By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

Two Syntax errors that I can't figure out

P: 14
I am trying to to write a program that will print the first 15 Fibonacci numbers each on one line with to lessons (print and produce)

the code that I have is below:
class numbers{
static int numbers[] = new int[15];
public static void produce(int Fib){

if(Fib < 2)
numbers[n] = n;
else
numbers[n] = numbers[n-1] + numbers[n-2];
}

public static void print(){

for(int k = 0; k < 15; k++)
System.out.println(numbers[k]);
}

public static void main(args[0]){
for(Integer i = 0; i < 15; i++)

produce (i);
print ();

}
the errors are
1) public static void main(args[0])
telling me an error under the "0"

and the other is I am missing and indentifier
Nov 16 '06 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
I am trying to to write a program that will print the first 15 Fibonacci numbers each on one line with to lessons (print and produce)

the code that I have is below:
Expand|Select|Wrap|Line Numbers
  1. class numbers{
  2.     static int numbers[] = new int[15];
  3.       public static void produce(int Fib){
  4.  
  5.     if(Fib < 2)
  6.       numbers[n] = n; // Where is N declared?
  7.     else
  8.       numbers[n] = numbers[n-1] + numbers[n-2];
  9.     }
  10.  
  11.     public static void print(){
  12.  
  13.     for(int k = 0; k < 15; k++)
  14.       System.out.println(numbers[k]);
  15.     }
  16.  
  17.     public static void main(args[0]){ // Should be "public static void main(String args[]) {
  18.       for(Integer i = 0; i < 15; i++)
  19.  
  20.     produce (i);
  21.     print ();
  22.  
  23.     }
the errors are
1) public static void main(args[0])
telling me an error under the "0"

and the other is I am missing and indentifier
You never declared N, yet you use it. Maybe you should use Fib instead of N.

Also, the main() function expects a class type, then a name - args[] was the name, and there was no type with it. It should be String args[]
Nov 16 '06 #2

P: 26
Hello

Your code only needs small modification:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class numbers{
  3. static int numbers[] = new int[15];
  4. public static void produce(int n){
  5.  
  6. if(n < 2)
  7. numbers[n] = n;
  8. else
  9. numbers[n] = numbers[n-1] + numbers[n-2];
  10. }
  11.  
  12. public static void print(){
  13.  
  14. for(int k = 0; k < 15; k++)
  15. System.out.println(numbers[k]);
  16. }
  17.  
  18. public static void main(String args[]){
  19. for(Integer i = 0; i < 15; i++)
  20.  
  21. produce (i);
  22. print ();
  23. }}
  24.  
alternatively you could do it like so with only one loop using a recursive method

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Fibonacci {
  3.     public static int fib(int n) {
  4.         if (n == 0) return 0;
  5.         if (n == 1) return 1;
  6.         return fib(n-1)+fib(n-2);
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.     for (int i=0; i<15; i++)
  11.         System.out.print(fib(i)+", ");
  12. }
  13.  
hope that helps

mark
Nov 17 '06 #3

P: 14
hjc
Thank you both very much for your help!!!! I got it working



Hello

Your code only needs small modification:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class numbers{
  3. static int numbers[] = new int[15];
  4. public static void produce(int n){
  5.  
  6. if(n < 2)
  7. numbers[n] = n;
  8. else
  9. numbers[n] = numbers[n-1] + numbers[n-2];
  10. }
  11.  
  12. public static void print(){
  13.  
  14. for(int k = 0; k < 15; k++)
  15. System.out.println(numbers[k]);
  16. }
  17.  
  18. public static void main(String args[]){
  19. for(Integer i = 0; i < 15; i++)
  20.  
  21. produce (i);
  22. print ();
  23. }}
  24.  
alternatively you could do it like so with only one loop using a recursive method

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Fibonacci {
  3.     public static int fib(int n) {
  4.         if (n == 0) return 0;
  5.         if (n == 1) return 1;
  6.         return fib(n-1)+fib(n-2);
  7.     }
  8.  
  9.     public static void main(String[] args) {
  10.     for (int i=0; i<15; i++)
  11.         System.out.print(fib(i)+", ");
  12. }
  13.  
hope that helps

mark
Nov 18 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.