Two Syntax errors that I can't figure out

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hjc
    New Member
    • Oct 2006
    • 14

    #1

    Two Syntax errors that I can't figure out

    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.prin tln(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
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by hjc
    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:
    Code:
    class numbers{
    	static int numbers[] = new int[15];
    	  public static void produce(int Fib){
    
    	if(Fib < 2)
    	  numbers[n] = n; // Where is N declared?
    	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]){ // Should be "public static void main(String args[]) {
    	  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
    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[]

    Comment

    • mharrison
      New Member
      • Nov 2006
      • 26

      #3
      Hello

      Your code only needs small modification:

      Code:
      class numbers{
      static int numbers[] = new int[15];
      public static void produce(int n){
      
      if(n < 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(String args[]){
      for(Integer i = 0; i < 15; i++)
      
      produce (i);
      print ();
      }}
      alternatively you could do it like so with only one loop using a recursive method

      Code:
      class Fibonacci {
      	public static int fib(int n) {
      		if (n == 0) return 0;
      		if (n == 1) return 1;
      		return fib(n-1)+fib(n-2);
      	}
      
      	public static void main(String[] args) {
          for (int i=0; i<15; i++)
              System.out.print(fib(i)+", ");
      }
      hope that helps

      mark
      Last edited by mharrison; Nov 17 '06, 03:40 PM. Reason: Put an extra < in code

      Comment

      • hjc
        New Member
        • Oct 2006
        • 14

        #4
        Thank you both very much for your help!!!! I got it working



        Originally posted by mharrison
        Hello

        Your code only needs small modification:

        Code:
        class numbers{
        static int numbers[] = new int[15];
        public static void produce(int n){
        
        if(n < 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(String args[]){
        for(Integer i = 0; i < 15; i++)
        
        produce (i);
        print ();
        }}
        alternatively you could do it like so with only one loop using a recursive method

        Code:
        class Fibonacci {
        	public static int fib(int n) {
        		if (n == 0) return 0;
        		if (n == 1) return 1;
        		return fib(n-1)+fib(n-2);
        	}
        
        	public static void main(String[] args) {
            for (int i=0; i<15; i++)
                System.out.print(fib(i)+", ");
        }
        hope that helps

        mark

        Comment

        Working...