1. Member
Join Date
Jul 2010
Posts
4
Rep Power
0

## 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. 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 08:47 PM. Reason: Added Java tutorial too.

3. Member
Join Date
Jul 2010
Posts
4
Rep Power
0
fanks for the help but i need to answer the question using an if statement? any ideas?:confused:

4. 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

#### Posting Permissions

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