Once again stuck with a Java homework: write a program which asks the user for upper limit (integer), then print the numbers in the following way (e.g.):
limit: 8 1 8 2 7 3 6 4 5
I'm thinking, first, two if loops:
if (limit % 2 == 0) { // limit is an even number.
and
if (limit % 2 != 0) { // limit is an odd number.
Then, under the first if loop, two for loops (e.g. limit = 8):
for (i = 1; i < limit / 2; i++) { // ascending from beginning, 1 2 3 4.
and
for (i = limit; i > limit / 2; i--) { // descending from end, 8 7 6 5.
And similarly, under the second if loop, two for loops (e.g. limit = 9):
for (i = 1; i < (limit + 1) / 2; i++) { // ascending from beginning, 1 2 3 4 5.
and
for (i = limit; i > (limit - 1) / 2; i--) { // descending from end, 9 8 7 6.
This is where I get stuck. It's easy to get the two sequences of numbers to print separately, but I can't figure out how to combine the for loops so that the numbers are printed alternatingly from the first and second sequence, as the homework tells me to do. Can someone give me hints on this? Thanks!

New Topic/Question
Reply



MultiQuote










|