Java Numbers work



  • I'm writing a program that shows an even and odd number at a certain interval from the user. And then I ran into one problem, and I'm choosing a range from 120 to 130, the program shows only even numbers, not even numbers.

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

    It's working normally until this part, and there's no clear numbers going on, and there's nothing going on.

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

    Can you tell me what the problem is? And yes, if not hard... How do we make the numbers clear from the end, not even from the beginning: Here's how it's been, like, a 10-30 interval.

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

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



  • Not exactly the answer to the question, but simply the example of how to deal with such a challenge 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 &lt;= max ) {
        System.out.println("Четные числа: " + IntStream.rangeClosed(min, max).filter(n -&gt; n % 2 == 0).boxed()
                        .sorted((x, y) -&gt; -1).collect(Collectors.toList()));
        System.out.println("Нечетные числа: " + IntStream.rangeClosed(min, max).filter(n -&gt; n % 2 != 0).boxed()
                        .collect(Collectors.toList()));
    }
    input.close();
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2