在数组中的奇偶值

huangapple go评论78阅读模式
英文:

Even/odd values in an array

问题

我遇到了一个布尔方法的问题,我想要检查数组是全偶数、全奇数还是既有偶数又有奇数。我输入了数组大小和数组值,然而 "isArrayEven" 方法却一直输出 "数组中的所有数字都是偶数",即使我的数组是 1、2、3,而 isArrayEven 应该返回 false。

import java.util.Scanner;

public class OddOrEven {
	
	public static boolean isArrayEven(int[] arrayValues, int arraySize){

		for(int i = 0; i <= arraySize - 1; i++)
		{
			if(arrayValues[i] % 2 == 0)
			{
				return true;
			}
		}
		return false;
	}

	public static boolean isArrayOdd(int[] arrayValues, int arraySize){

		for(int i = 0 ; i < arraySize ; i++)
        {
            if(arrayValues[i] % 2 == 1)
            {
                return true;
            }
        }
		return false;
	}

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);

		int arraySize = scan.nextInt();				
		int[] arrayValues = new int[arraySize];		

		for(int i = 0; i <= arraySize-1; i++)
		{
			arrayValues[i] = scan.nextInt();
		}
		
		if(isArrayEven(arrayValues,arraySize) == true)
		{
			System.out.println("数组中的所有数字都是偶数");
		}else if(isArrayOdd(arrayValues,arraySize) == true)
		{
			System.out.println("数组中的所有数字都是奇数");
		}else if(isArrayEven(arrayValues,arraySize) == false && isArrayOdd(arrayValues,arraySize) == false)
		{
			System.out.println("既有偶数又有奇数");
		}
	}

}
英文:

I am having trouble with a boolean method, I want to check of the array is all even, odd, or neither. I input the array size and array values, however the "isArrayEven" method keeps outputting "all numbers in array is even", even if my array were to be 1,2,3 and isArrayEven is supposed to be false.

import java.util.Scanner;
public class OddOrEven {
public static boolean isArrayEven(int[] arrayValues, int arraySize){
for(int i = 0; i &lt;= arraySize -1; i++)
{
if(arrayValues[i] % 2 == 0)
{
return true;
}
}
return false;
}
public static boolean isArrayOdd(int[] arrayValues, int arraySize){
for(int i = 0 ; i &lt; arraySize ; i++)
{
if(arrayValues[i] % 2 == 1)
{
return true;
}
}
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int arraySize = scan.nextInt();				
int[] arrayValues = new int[arraySize];		
for(int i = 0; i &lt;= arraySize-1; i++)
{
arrayValues[i] = scan.nextInt();
}
if(isArrayEven(arrayValues,arraySize) == true)
{
System.out.println(&quot;all numbers in array is even&quot;);
}else if(isArrayOdd(arrayValues,arraySize) == true)
{
System.out.println(&quot;all numbers in arrat is odd&quot;);
}else if(isArrayEven(arrayValues,arraySize) == false &amp;&amp; isArrayOdd(arrayValues,arraySize) == false)
{
System.out.println(&quot;both have even and odd&quot;);
}
}
}

I

答案1

得分: 2

你的返回过早了。你需要稍微修改一下你的 if 语句。看一下这个例子:

public static boolean isArrayEven(int[] arrayValues, int arraySize){

    for(int i = 0; i <= arraySize - 1; i++)
    {
        if(arrayValues[i] % 2 != 0)
        {
            return false;
        }
    }
    return true;
}

public static boolean isArrayOdd(int[] arrayValues, int arraySize){

    for(int i = 0 ; i < arraySize ; i++)
    {
        if(arrayValues[i] % 2 == 0)
        {
            return false;
        }
    }
    return true;
}

在这里,我稍微修改了 if 语句。

isArrayEven 方法中,如果出现了奇数,我会返回 false。如果没有出现奇数,我会返回 true

isArrayOdd 方法中,如果出现了偶数,我会返回 false。如果没有出现偶数,我会返回 true

英文:

You are returning too early. You need to flip and modify your if-statements a bit. Have a look at this:

public static boolean isArrayEven(int[] arrayValues, int arraySize){
for(int i = 0; i &lt;= arraySize -1; i++)
{
if(arrayValues[i] % 2 != 0)
{
return false;
}
}
return true;
}
public static boolean isArrayOdd(int[] arrayValues, int arraySize){
for(int i = 0 ; i &lt; arraySize ; i++)
{
if(arrayValues[i] % 2 == 0)
{
return false;
}
}
return true;
}

Here I have modified the if-statements a bit.

In the isArrayEven method, I return false on the first occurrence of an odd number. If no odd number occurs, I return true.

In the isArrayOdd method, I return false on the first occurrence of an even number. If no even number occurs, I return true.

答案2

得分: 0

在我看来,你的测试和返回值应该颠倒过来。现在,当你找到一个偶数时,你会返回"the entire array is even"=true。在"判断是否为偶数"的函数中,你必须检查所有的值,或者在找到任何一个不是偶数的值时返回false。

一般来说,我建议你在遍历数组之前先初始化两个布尔值;例如"all_are_odd"和"all_are_even",并将它们初始化为true;

然后只需遍历整个数组一次,如果遇到不满足"all_are_x"的值,只需将"all_are_odd" / "all_are_even"设置为false。

你可以通过在"all_are_odd" / "all_are_even"都为false时跳出循环来优化代码。

在这次迭代之后,你现在可以输出是否全部为偶数、全部为奇数,或介于两者之间。

特殊情况当然是空集。

英文:

It seems to me your tests and return values should be inverted. You will now return "the entire array is even"=true when you find just one even number. You have to check all values, or return false when you find any value that is not even in the "is even" function.

In general I would suggest it would be better to iterate your array once and initialize two bools before iterating; eg. "all_are_odd" and "all_are_even" and initialize these to true;

Then just iterate the full array once and only set all_are_x to false if you come across a value that does not meet all_are_x.

You could optimize by breaking out of the loop once both all_are_odd / all_are_even are false.

After this iteration you can now output whether all are even, all are odd, or something in between.

Edge case is of course the empty set.

答案3

得分: 0

你的错误在于当你找到偶数或奇数时,你会返回true或false。这意味着假设你有一个整数数组[2, 1, 3, 5]。当你的isEven方法对这个数组运行时,它会返回true,但是数组的第1、第2和第3个值是奇数。你现在需要做的是与你目前所做的相反。

public boolean isEven(int arr[]){
    for(int i=0; i<arr.length; ++i)
       if(arr[i]%2 != 0) return false;
    return true;
}
英文:

your wrong is you are returning true or false whenever you find even number or odd number. That means lets say you have an array of integer numbers [2, 1, 3, 5]. When your isEven method ran for this array it will return true, but the 1st, 2nd and 3rd values of the array is odd. What you need to do is the opposite of you are doing right now.

public boolean isEven(int arr[]){
    for(int i=0; i&lt;arr.length; ++i)
       if(arr[i]%2 !=0) return false;
    return true;
}

答案4

得分: 0

public static boolean isArrayEven(int[] arrayValues, int arraySize){
for(int i = 0; i <= arraySize - 1; i++) {
if(arrayValues[i] % 2 != 0) { // if found an odd number
return false;
}
}
return true; // if not found any odd number means all number is even
}

英文:

You should send false if you find odd number in array and return true if all element in array is even.

public static boolean isArrayEven(int[] arrayValues, int arraySize){
for(int i = 0; i &lt;= arraySize -1; i++) {
if(arrayValues[i] % 2 != 0) { // if found an odd number
return false;
}
}
return true; // if not found any odd number means all number is even
}

答案5

得分: 0

我在某种程度上同意Richard的观点,即你需要一些布尔变量,但我会采取一种不同的方法:

boolean containsEven = false;
boolean containsOdd = false;

// 循环遍历数组中的所有元素,一旦找到偶数,将第一个布尔变量更改为true。同样地,一旦找到奇数,将第二个布尔变量更改为true。如果两个布尔变量都变为true,甚至可以考虑提前结束循环,因为从检查数组的其余部分不会再学到更多信息。

for (int num : array) {
    if (num % 2 == 0) {
        containsEven = true;
    } else {
        containsOdd = true;
    }

    if (containsEven && containsOdd) {
        break;
    }
}

// 然后,在你的if/else块中使用这些布尔变量。
英文:

I sort of agree with Richard in that you need some boolean variables, but I would take a different approach:

boolean containsEven = false;
boolean containsOdd = false;

Cycle through all the elements in the array, and as soon as you find an even number change the first boolean to true. Likewise, change the second boolean to true once an odd number has been found. You might even consider breaking the cycle early if both booleans become true, as there would be nothing further to learn from inspecting the rest of the array.

Then, use these booleans in your if/else block.

huangapple
  • 本文由 发表于 2020年9月2日 14:11:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63699643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定