5 Replies - 468 Views - Last Post: 06 February 2017 - 09:21 PM Rate Topic: -----

#1 Beginner4ever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 17

Need Help on Questions

Posted 06 February 2017 - 06:01 PM

I can't for the life of me figure this out, and I'm close to having to drop out of the class.

1. Write a method called printPowersOf2 that accepts the maximum number as an argument and prints each power of 2 from 2^0 up to the maximum number. For example, consider the following calls:

printPowersOf2(3);
printPowersOf2(10);

Output should look like this:

1 2 4 8
1 2 4 8 16 32 64 128 256 512 1024

.. I couldn't succesfully write a method without an error.. my code looked like this but won't run:
public static void double (int max)

Math.pow(2,i);

 for(int i=0;i<=10;i++);
        System.out.println(Math.pow(2,i));




I really need help on this and the teacher can't seem to explain it to me.

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help on Questions

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,214
  • Joined: 12-June 08

Re: Need Help on Questions

Posted 06 February 2017 - 06:31 PM

Can't run how? Errors, where?

For loop conditions do not need semi colons after them. I am thinking the error messages tell you as much.

5 for(int i=0;i<=10;i++);

Was This Post Helpful? 0
  • +
  • -

#3 Beginner4ever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 17

Re: Need Help on Questions

Posted 06 February 2017 - 06:53 PM

Thank you I was able to figure out at least the symtax error:

for(int i=0; i<=3; i++)
           System.out.println(Math.pow(2,i));
    
      
        for(int i=0; i<=10; i++)
           System.out.println(Math.pow(2,i));
    }



But how would I create a method named powersOf2? The teacher never explained the criteria of methods, classes, objects.
Was This Post Helpful? 0
  • +
  • -

#4 Beginner4ever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 17

Re: Need Help on Questions

Posted 06 February 2017 - 07:00 PM

I know you really didn't do much but thank you. I tend to go catastrophic mode when I can't figure something out and give up.

I figured it out:

public class JavaApplication3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        printPowersOf2();
        
        
    }
    
    static void printPowersOf2() {
    for(int i=0; i<=3; i++)
           System.out.println(Math.pow(2,i));
    
      
        for(int i=0; i<=10; i++)
           System.out.println(Math.pow(2,i));
}



It may not seem like much but to me this is a big victory. thanks
Was This Post Helpful? 0
  • +
  • -

#5 Beginner4ever   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-February 17

Re: Need Help on Questions

Posted 06 February 2017 - 07:01 PM

I know you really didn't do much but thank you. I tend to go catastrophic mode when I can't figure something out and give up.

I figured it out:

public class JavaApplication3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        printPowersOf2();
        
        
    }
    
    static void printPowersOf2() {
    for(int i=0; i<=3; i++)
           System.out.println(Math.pow(2,i));
    
      
        for(int i=0; i<=10; i++)
           System.out.println(Math.pow(2,i));
}



It may not seem like much but to me this is a big victory. thanks

I know you really didn't do much but thank you. I tend to go catastrophic mode when I can't figure something out and give up.

I figured it out:

public class JavaApplication3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        printPowersOf2();
        
        
    }
    
    static void printPowersOf2() {
    for(int i=0; i<=3; i++)
           System.out.println(Math.pow(2,i));
    
      
        for(int i=0; i<=10; i++)
           System.out.println(Math.pow(2,i));
}



It may not seem like much but to me this is a big victory. thanks
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,214
  • Joined: 12-June 08

Re: Need Help on Questions

Posted 06 February 2017 - 09:21 PM

Quote

I know you really didn't do much but thank you.

Snap! Rhythm is a dancer..
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1