简体   繁体   English

按降序打印奇数

[英]Print odd numbers in a descending order

The program needs to take an odd number and output it in a descending order For example: if the input is 11 the output needs to be 11 , 9 , 7 , 5 , 3, 1. 程序需要取一个奇数并以降序输出。例如:如果输入为11,则输出必须为11,9,7,5,3,1。

I tried using a for loop but I can only seem to get it to work with even numbers not odd numbers 我尝试使用for循环,但似乎只能让它与偶数而不是奇数一起使用

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int number = input.nextInt();

    for (int i = number - 1; i >= 0; i--) {

        if (i % 2 == 0) {
            int descend = i;
            System.out.println(descend + " ");
        }
    }
}

The output is the number in descending order but as even only. 输出是降序编号,但仅偶数。 If I add a 1 into the descend variable the numbers would seem to descend in an odd manner but its not ideal. 如果我在下降变量中加上1,数字似乎会以一种奇怪的方式下降,但并不理想。

This line returns true if the number is even: 如果数字为偶数,则此行返回true:

if (i % 2 == 0) {

If you want to know when the number is odd: 如果您想知道数字何时为奇数:

if (i % 2 != 0) {

Also, why are you starting your count at 1 less than the input value: 另外,为什么要从小于输入值的1开始计数:

int i = number - 1;

I think you want to do this: 我认为您想这样做:

for (int i = number; i > 0; i--) {  // tests for numbers starting at the input and stopping when i == 0

Asking if the number % 2 is equal to zero is basically asking if the number is even, so what you really have to do is ask if the number % 2 is not equal to zero, or equal to 1 询问数字%2是否等于零基本上就是询问数字是否为偶数,所以您真正要做的就是询问数字%2是否等于零或等于1。

if (i % 2 != 0) {
    int descend = i;
    System.out.println(descend + " ");
}

Also, there's no need to subtract 1 from the user input so your for loop can be written like this 另外,无需从用户输入中减去1,因此您的for循环可以这样编写

for (int i = number; i >= 0; i--) {

    if (i % 2 == 0) {
        int descend = i;
        System.out.println(descend + " ");
    }
}
 public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter an an odd number: ");
    int number = input.nextInt();
    while(number%2==0){
        System.out.print("Number must be odd number:" +
                "(Ex:1, 3,5)\nTry again: ");
        number=input.nextInt();
    }

    for (int i = number; i >= 0; i--) {
        if(number%2!=0){
            System.out.println(number);}
        number-=1;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用集合的情况下按升序打印偶数和按降序打印奇数 - How to print even numbers in ascending order and odd numbers in descending order without using collection 按升序对所有偶数进行排序,然后按集合中的降序对所有奇数进行排序 - Sort all even numbers in ascending order and then sort all odd numbers in descending order in a collection 输入任意数字并使用while循环以降序打印所有奇数 - Input any number and print all the odd number in descending order using while loop 使偶数和奇数线程在Java中以自然顺序打印数字 - Make even and odd threads to print numbers in natural order in Java 按顺序排列偶数和奇数 - Sort even and odd numbers in order QuickSort(分区Hoare)的修改,先偶数降序,再奇数降序 - Modification of QuickSort (partition Hoare), first even numbers descending, then odd numbers descending 如何将数组中的数字按降序排列? - how to shift numbers in an array to be in descending order? 仅使用 if/else 在 Java 中按降序排列的数字 - Numbers in descending order in Java using if/else only 如何在Java中按降序打印结果? - How to print the result in descending order in java? 如何按降序打印媒体文件 - How to Print Media Files in DESCENDING Order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM