简体   繁体   English

Java中的布尔值

[英]Boolean in Java

Prompt: You can test to see if an integer, x, is even or odd using the Boolean expression (x / 2) * 2 == x. 提示:您可以使用布尔表达式(x / 2)* 2 == x来测试整数x是偶数还是奇数。 Integers that are even make this expression true, and odd integers make the expression false. 偶数使该表达式为真,奇数整数使该表达式为假。 Use a for loop to iterate five times. 使用for循环迭代五次。 In each iteration, request an integer from the user. 在每次迭代中,向用户请求一个整数。 Print each integer the user types, and whether it is even or odd. 打印用户键入的每个整数,以及它是偶数还是奇数。 Keep up with the number of even and odd integers the user types, and print “Done” when finished, so the user won't try to type another integer. 跟上用户键入的偶数和奇数整数的数量,并在完成后打印“ Done”,因此用户不会尝试键入其他整数。 Finally, print out the number of even and odd integers that were entered. 最后,打印出输入的偶数和奇数整数的数量。

Here is the code I have so far: 这是我到目前为止的代码:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter an integer.");
    int x = in.nextInt();
    boolean even;
    for (int i = 0; i == 5; i++) {
        if ((x / 2) * 2 == x) {
            even = true;
            System.out.println(x + " is even.");
        }
        if ((x / 2) * 2 != x) {
            even = false;
            System.out.println(x + " is odd.");
        }
    }
}

Not looking for a solution, just some help as to what I need to do. 不寻找解决方案,只是对我需要做的一些帮助。 Really confused about the whole Boolean thing. 对整个布尔值真的很困惑。

This seems to be like your homework. 这似乎就像您的作业。

Seems like your 'boolean even' is not even being used, I would suggest that you don't declare nor use it. 似乎甚至没有使用“布尔甚至”,我建议您不要声明或使用它。 Use x = x%2 to get the number if it is even or odd is better. 如果x等于x或奇数更好,请使用x = x%2来获取数字。 If it is even x should be 0, if it is odd x should be 1. % is equivalent to MOD 如果为偶数,x应该为0,如果为奇数,x应该为1。%等于MOD

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
int even = 0;   // keep tracks the number of even
int odd = 0;    // keep tracks the number of odd
for (int i = 0; i < 5; i++) {
    System.out.println("Enter an integer.");
    x = in.nextInt();
    if (x % 2 == 0) {
        even++;
        System.out.println(x + " is even.");
    }
    if (x % 2  == 1) {
        odd++;
        System.out.println(x + " is odd.");
    }
}
System.out.println("Done");
System.out.println("Evens: " + even "\nOdds: " + odd);
}

This code should be the answer for your homework requirement. 此代码应该是您的家庭作业要求的答案。 Your in.nextInt() should be inside the for loop since you need to request the user 5 times. 您的in.nextInt()应该位于for循环内,因为您需要请求用户5次。 Not only that, your loop should be < 5 as it will loop 5 times from 0, 1, 2, 3, 4 不仅如此,您的循环应<5,因为它将从0、1、2、3、4循环5次

Well, your loop won't fire; 好吧,您的循环不会触发; i == 5 is always going to be false every time you reach the loop. 每次到达循环时, i == 5始终为假。

What you may want to change your loop statement to be would be: 您可能希望将循环语句更改为:

for (int i = 0; i <= 5; i++) {
    // code
}

Further, by virtue of the way Java evaluates branches, the variable even may not have been initialized. 此外,凭借Java评估分支的方式,该变量even可能尚未初始化。 You need to instantiate it with a value. 您需要使用一个值实例化它。

boolean even = false;

Finally, the most straightforward way to tell if a number is even is to use the modulus operator. 最后,判断数字是否为偶数的最直接方法是使用模运算符。 If it's divisible by two, it's even. 如果被二整除,则为偶数。 Otherwise, it's odd. 否则,这很奇怪。

if (x % 2 == 0) {
    // even, do logic
} else {
    // odd, do logic
}

You are missing a requirement from the assignment - that is, the ability to keep a running tally of the number of odd and even numbers, but I leave that as an exercise to the reader. 缺少从分配的要求-即保持奇数和偶数的个数的流水账的能力,但我将它作为一个练习留给读者。

The part that you're missing is keeping track of how many even and how many odd numbers have been encountered. 您缺少的部分是跟踪遇到的偶数和奇数个数。 You'll want two separate int variables for this, which you'll declare before your main loop. 为此,您需要两个单独的int变量,您将在主循环之前声明这些变量。

int numEvens = 0;
int numOdds = 0;

Then, in the branches where you work out whether the entered number is odd or even, you'll increment one or other of these numbers. 然后,在计算输入数字是奇数还是偶数的分支中,将增加一个或另一个这些数字。

Lastly, at the end of your program, you can print them both out in a message. 最后,在程序结束时,您可以将它们同时打印出来。

if you want to do this with java boolean..i think this might help you 如果您想用java boolean..i做到这一点,我认为这可能对您有帮助

package stackOverFlow;

public class EvenOddNumber {


public boolean findEvenOdd(int num) {
    if (num % 2 == 0) {
        return true;

    }
    else {

        return false;
    }

}
}




import java.util.Scanner;

public class Demo {
public static void main(String[] args) {
    int num;
    EvenOddNumber e = new EvenOddNumber();
    System.out.print("Enter a number:");
    Scanner scan = new Scanner(System.in);
    num = scan.nextInt();

    System.out.println( num+"  is even number?: " + e.findEvenOdd(num));

}

}

A simpler way to find even and odd values is to divide number by 2 and check the remainder: 查找偶数和奇数的一种更简单的方法是将数字除以2,然后检查余数:

if(x % 2 == 0) // remainder is 0 when divided by 2
{
//even num
}
else
{
//odd num
}

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

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