My current courseork involves loops in Java. The story of my life is such that once I change something I get hit with a roadblock.
"You want to be a programmer?"
"I do!"
"Well, here's a assignment that will drive you nucking futs."
Alright. My task is to use a single while loop to print out even and odd numbers between 50 and 100. The even numbers will be labled and printed on the same line while the odd numbers will follow the same format. For example:
Even numbers between 0 and 50: 50, 52, 54, 56, 58, ... , 100
Odd numbers between 0 and 50: 51, 53, 55, 57, 59, ... , 99
I have nailed the code to display the numbers on the same line but it only works properly when I disable one portion of the loop. If I leave all of the code intact, the output is as follows:
50
51, 52
53, 54
.
.
99, 100
Here's the code with some personal annotations.
Code: Select all
public static void main(String args[])
{
//start at 50
int evenNumber = 50;
int oddNumber = 50;
//execute the commands when both the even and odd numbers are less than or equal to 100
while ((evenNumber <= 100) && (oddNumber <= 100))
{
if (evenNumber % 2 == 0)
{
System.out.print(evenNumber + ", ");
}
evenNumber++;
//if I disable the if statement for the odd number, everything works fine. The same can be said if I disable the if statement for
//the even number and leave hte code for the odd
if (oddNumber % 2 != 0)
{
System.out.println();
System.out.print(oddNumber + ", ");
}
oddNumber++;
}
}