简体   繁体   English

为什么我的偶数和奇数打印程序只打印奇数次?

[英]Why does my even and odd printing program print just print odd many times?

I'm trying to make a java program that takes a number given by the user, and displays whether it is odd or even. 我正在尝试制作一个采用用户给定数字的Java程序,并显示它是奇数还是偶数。 I am having trouble, i think it might be tht my logic is wrong idk. 我遇到了麻烦,我认为我的逻辑可能是错误的idk。 It just prints the never ending message "odd" 它只是打印永无止境的消息“奇数”

import java.util.Scanner;

public class Questions {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Your number");
    int number = input.nextInt();
    for (int i = 0; i <= number; i = +2)
      if (number == i) {
        System.out.println("even");
      } else {
        System.out.println("odd");
      }
  }
}

You have an endless for statement, because you don't change a loop variable i . 您有无休止的for语句,因为您不更改循环变量i You might try the following pieces of code to decide your task: 您可以尝试以下代码来决定您的任务:

System.out.println(number % 2 == 0 ? "even" : "odd");

or 要么

System.out.println((number & 1) == 0 ? "even" : "odd");

Those lines will give same result: 这些行将产生相同的结果:

(number = 2)
even

Of course, in your case, you might change this line for (int i = 0; i <= number; i = +2) to the next line for (int i = 0; i <= number; i += 2) , but an output won't give an expected result: 当然,在你的情况下,你可能会改变这条线for (int i = 0; i <= number; i = +2)到下一行for (int i = 0; i <= number; i += 2) ,但输出不会产生预期的结果:

(number = 2)
odd
even

In your for loop you have the assignement 在for循环中,您可以进行分配

i =+ 2

This sets the variable i to positive 2. To add 2 to the variable use: 这会将变量i为正2。要将2加到变量中,请使用:

i += 2

This would print "odd" as long as i is smaller than the input value and print a final "even" if the value is event or "odd" if the value is odd. 只要i小于输入值,这将打印“奇数”,如果值是事件,则将打印最终的“偶数”,如果值是奇数,则打印“奇数”。

Btw to test if a value is a multiple of another value you can use 顺便说一句,以测试一个值是否可以使用另一个值的倍数

if ((value % otherValue) == 0) {}

so in your example 所以在你的例子中

if ((number % 2) == 0) {
    System.out.println("even");
} else {
    System.out.println("odd");
}

or even shorter 甚至更短

System.out.println((number % 2) == 0 ? "even" : "odd");

Your check for an odd / even number is wrong and could be optimised. 您检查的奇/偶数是错误的,可以对其进行优化。 Use the modulus operator instead: 请使用模运算符:

if (number % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}

The modulus gives you the remainder of the division of the operands. 模为您提供操作数除法的余数。 If you divide by 2 and the remainder is 0 - you have an even number. 如果您将其除以2,而余数为0,则您有一个偶数。

you are make mistake in the for statement 您在for陈述中犯了错误

for (int i = 0; i <= number; i = +2)// this will not increment the counter, it will set it every time with  2 positive

for (int i = 0; i <= number; i +=2)// this will increment the counter by 2 in every iteration 

also you should try to find the problem by debugging or use some print statements after loop or in condition to troubleshot this issue. 您也应该尝试通过调试来发现问题,或者在循环后或以解决此问题的方式使用一些打印语句。

This program never terminates. 该程序永远不会终止。

for(int i = 0; i <= number; i=+2)

The offending code is that last item. 令人讨厌的代码是最后一个项目。 Incrementing i by 2 looks like this: 将i递增2看起来像这样:

i += 2;

If you write it like this: 如果您这样写:

i =+ 2;

Then it will just repeatedly assign the value '2' to i. 然后,它将重复为i分配值“ 2”。

Of course, as the other posters already pointed out, this isn't how you should test evenness/oddness. 当然,正如其他张贴者已经指出的那样,这不是测试均匀度/奇数的方式。 You should either perform a modulo check: 您应该执行模检查:

if(i % 2 == ) <even>
else <odd>

Or simply perform a bitwise comparison on the last digit. 或者只是对最后一位进行按位比较。

if(i & 1 == 0) <even>
else <odd>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM