I write a program that shows even and odd numbers in a certain interval specified by the user. And then I came across one problem, I select the interval, say, from 120 to 130, the program shows only even numbers, and odd numbers do not show

System.out.println("Четные числа"); for (; a <= b; a++) { if (a % 2 == 0) { System.out.print(a); System.out.print(","); } } 

It works fine to this part, then odd numbers follow and nothing is highlighted here.

  System.out.println(""); System.out.println("Нечетные числа"); for (; a <= b; a++) { if (a % 2 != 0) { System.out.print(a); System.out.print(","); } } 

Can you tell what the problem is? And yes, if it is not difficult .. How to make the even numbers show from the end, and the odd numbers from the beginning: like this, for example: choose the interval from 10 - 30

11,13,15,17,19,21,23,25,27,29

30,28,26,24,22,20,18,16,14,12,10

Closed due to the fact that off-topic participants are Alexey Shimansky , zRrr , aleksandr barakin , user194374, enzo 13 Jun '16 at 15:56 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Alexey Shimansky, zRrr, aleksandr barakin, Community Spirit, enzo
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 9
    We can. 1. When the code comes to the second cycle, а already more b . Get a separate variable for the loop counter: for (int i = a; i <= b; i++) { if (i % 2... 2. for (int i = b; i >= a; i--) { ... - Igor
  • Vooot worked)) Write it in response) You were right) And yes, I made the edit in the question, help me with this too if you know :) - E1mir
  • I will not write. This is so trivial that it deserves maximum comment. - Igor
  • well ok, thanks for the answer) - E1mir

2 answers 2

Not exactly the answer to the question, but just an example of how to solve such a puzzle using Java 8.

 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Введите начальную страницу: "); int min = Integer.parseInt(input.next()); System.out.print("Введите конечную страницу: "); int max = Integer.parseInt(input.next()); if ( min <= max ) { System.out.println("Четные числа: " + IntStream.rangeClosed(min, max).filter(n -> n % 2 == 0).boxed() .sorted((x, y) -> -1).collect(Collectors.toList())); System.out.println("Нечетные числа: " + IntStream.rangeClosed(min, max).filter(n -> n % 2 != 0).boxed() .collect(Collectors.toList())); } input.close(); } 
  • 2
    You can also use Collectors.partitioningBy to divide into two lists for one stream call. - zRrr
  • Yes, it worked) Thanks for the reply) - E1mir
  StringBuilder odd = new StringBuilder(); StringBuilder even = new StringBuilder(); for (; a <= b; a++) { if (a % 2 == 0) { even.append(a).append(","); }else { odd.append(a).append(","); } } System.out.println("Четные числа"); System.out.println(even.toString().substring(0,even.length()-1)); System.out.println("Нечетные числа"); System.out.println(odd.toString().substring(0,odd.length()-1)); 
  • Thank you) It works too ) - E1mir