I have this code that if i enter a number for example :
Enter a number: 1 2 3
output is
Even:2
Odd:1
Odd:3
but i like it like this:
Enter a number: 1 2 3
Even: 2
Odd : 1 3
Here is My code hope you could help me with this
I have this code that if i enter a number for example :
Enter a number: 1 2 3
output is
Even:2
Odd:1
Odd:3
but i like it like this:
Enter a number: 1 2 3
Even: 2
Odd : 1 3
Here is My code hope you could help me with this
At the start you don't know how many evens and odds there may be, so you can build up two Strings as you process the numbers and print them only at the end - like this pseudocode:
evens = "", odds = "";
for each number in the input {
if number is even append it to evens, else append it to odds
}
print evens and odds
OR
you could split the loop into two. In the first loop print all the even numbers then, in the second loop print all the odd numbers. If you use print rather than println the output will all be on the same line.
how can i append or add to even? i can't catch the logic sorry
yea i tried that already but it goes like this even:2 odd:1 odd:1
You can append anything to a String with the + operator, If the thing you're appending is not a String java will convert it for you...
String s = "The even numbers are ";
s = s + 2;
s = s + " and " + 4 ;// s is now "The even numbers are 2 and 4"
(You may find the second approach I just added above easier - it's up to you)
thanks alot. i got it already :)