Results 1 to 4 of 4
  1. #1
    newstart is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Exclamation unsolvable question

    I am only new to java recently! I am struggling slightly with the more advanced questions. I am just wondering if someone would be able to help me out and answer this question so i can see where i'm going wrong.


    QUESTION: MODIFY THE FOLLOWING CODE TO INCLUDE AN IF STATEMENT THAT ALLOWS ONLY THE ODD VALUES IN THE ARRAY A TO BE PRINTED.

    int i;
    int [] A = {2, 1, 7, 8, 10};
    for(i=0; i<A.length; i++)
    A[i] = 2*i;
    for(i=0; i<A.length; i++)
    System.out.print(a[i] + " ");
    System.out.println();

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Because of the nature of single-line for loops, I would instead suggest using curly braces around your for loop code to make this a lot easier.

    Eg:
    Java Code:
    for(i=0; i<A.length; i++){
        System.out.print(a[i] + " ");
        System.out.println();
    }
    That tells the program that everything between the {} is what is meant to be executed.

    As for the printing of only odd numbers, you'll have to use modulus (or division and rounding, but that's uglier) to determine whether or not the number is odd.
    This is a PHP tutorial but the concept is the same: Find Odd Or Even Numbers | Learn PHP Online
    (Here is one in Java: http://www.rgagnon.com/javadetails/java-0488.html)

    Buen suerte!
    Last edited by Zack; 07-22-2010 at 07:47 PM. Reason: Added Java tutorial too.

  3. #3
    newstart is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Question

    fanks for the help but i need to answer the question using an if statement? any ideas?:confused:

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Did you look at the links Zack posted?

    We realise you are new to Java, but you can't expect us to do the work for you.
    You need to show us what you have done, and say why that isnt doing what you expect it to do.

    You have just posted us the question and expected us to answer it for you. Since you are new to java, we wont help you in this way. You don't learn anything that way.

    The code you need was posted by Zack in his link:
    Java Code:
    if (x % 2 == 0) {
      // even 
    }
    
    if (x % 2 != 0) {
      // odd
    }
    Its now up to you to put that into your code! If you still cant do it, then feel free to ask.
    If you do ask, be sure to show us the new code, and how you have tried to implement it.

    Berkeleybross

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. RMI question
    By alvandrood in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 12:36 PM
  3. Hello everyone and my first question
    By htetnaing in forum New To Java
    Replies: 3
    Last Post: 01-26-2009, 03:49 PM
  4. JNI question
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-24-2007, 10:18 AM
  5. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •