猿问

如何在任何数字之间找到偶数和奇数?

我正在制作一个程序作为我在 java 中学习的培训,它可以查看用户输入的任何数字之间的偶数和奇数,如果我在开始时输入偶数,它会显示消息的问题


你没有输入偶数


如果用户在开始时输入了一个奇数,我只是希望他首先查看此消息


public static void main(String[] args) {

  isevennumber(1, 5);

}


public static void isevennumber(int startwith, int endwith) {


    for (int i = startwith; i <= endwith; i++) {


        if (i % 2 == 0) {

            System.out.println("you have entered an even number which is " + i);


        } else  {

            System.out.println("you haven't entered an even number");


        }

    }


}

输出是


you haven't entered an even number

you have entered an even number which is 2

you haven't entered an even number

you have entered an even number which is 4

you haven't entered an even number


抱歉有任何错误,这是我在这里的第一篇文章


泛舟湖上清波郎朗
浏览 161回答 3
3回答

四季花海

只需再添加一个if以检查这是否是第一次迭代:public static void isevennumber(int startwith, int endwith) {&nbsp; &nbsp; for (int i = startwith; i <= endwith; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("you have entered an even number which is " + i);&nbsp; &nbsp; &nbsp; &nbsp; else if (i == startwith)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("you haven't entered an even number");&nbsp; &nbsp; }}

隔江千里

这里的问题是,每次在循环中数字不是奇数时,它都会打印出您没有输入偶数。你可以这样试试:&nbsp; public static void main(String[] args) {&nbsp; &nbsp; isevennumber(1, 5);&nbsp; }&nbsp; public static void isevennumber(int startwith, int endwith) {&nbsp; &nbsp; &nbsp; List<Integer> evenNumbers = new ArrayList();&nbsp; &nbsp; &nbsp; for (int i = startwith; i <= endwith; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evenNumbers.add(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (evenNumbers.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("you haven't entered an even number");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println("you have entered the following even numbers " + evenNumbers);&nbsp; }这将打印出:&nbsp;you have entered the following even numbers [2, 4]

BIG阳

您错过了一个步骤,这里的问题是每次循环中数字不是奇数时,它都会打印您没有输入偶数。以下是适合您的正确解决方案。&nbsp; &nbsp; public static void isevennumber(int startwith, int endwith) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = startwith; i <= endwith; i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("you have entered an even number which is " + i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (i == startwith)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("you haven't entered an even number");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答