找到第一次出现的奇数,并对其进行计数 import java.util.*; 公共类优先事件{ 公共静态void main(字符串[]args){ int[]x={2,4,6,7,4,3,2,7,6,7,7}; int i; 显示器(x); System.out.printf(“\n第一个奇数的出现=%3d\n”,firstOddCount(x)); } 专用静态无效显示(int[]x){ int i; 系统输出打印(“数组:”); 对于(i=0;i

找到第一次出现的奇数,并对其进行计数 import java.util.*; 公共类优先事件{ 公共静态void main(字符串[]args){ int[]x={2,4,6,7,4,3,2,7,6,7,7}; int i; 显示器(x); System.out.printf(“\n第一个奇数的出现=%3d\n”,firstOddCount(x)); } 专用静态无效显示(int[]x){ int i; 系统输出打印(“数组:”); 对于(i=0;i,java,arrays,Java,Arrays,我试图在已提供的数组中找到第一个奇数。我的程序有什么问题?我似乎无法让程序计算第一次奇数出现。您的代码如下: import java.util.*; public class FirstOddOccurrence { public static void main(String[] args) { int[] x = {2, 4, 6, 7, 4, 3, 2, 7, 6, 7, 7}; int i; display(x);

我试图在已提供的数组中找到第一个奇数。我的程序有什么问题?我似乎无法让程序计算第一次奇数出现。

您的代码如下:

import java.util.*;

public class FirstOddOccurrence {
    public static void main(String[] args) {
        int[] x = {2, 4, 6, 7, 4, 3, 2, 7, 6, 7, 7};
        int i;

        display(x);

        System.out.printf("# Occurrences of first odd = %3d\n", firstOddCount(x));
    }

    private static void display(int[] x) {
        int i;

        System.out.print("Array: ");
        for (i = 0; i < x.length; i++) {
            if (i < x.length - 1)
                System.out.printf("%3d, ", x[i]);
            else
                System.out.printf("%3d\n", x[i]);
        }
    }

    public static int odd(int[] x) {
        int i;
        int y;
        for (i = 0; i < x.length; i++) {
            y = x[i] % 2;
            if (y == 1) {
                return x[i];
            } else {
                return 0;
            }
        }
        return x[i];
    }

    public static int firstOddCount(int x[]) {
        int i;
        int c = 0;
        for (i = 0; i < x.length; i++) {
            if (x[i] == odd(x))
                c++;

        }
        return c;
    }
}
不起作用-如果测试的数字为偶数,则立即返回
0
。相反,您希望跳过这些偶数,并等待奇数出现。最后,如果没有找到任何奇数,则返回
0
。以下是
odd()
的更正版本:

inti;
int-y;
对于(i=0;i
如果您试图从组中获取一个元素,当您的条件第一次匹配时,您应该使用“break”,否则它将提供所有…

安德烈的解决方案解决了您的问题;如果x[0]为偶数,则奇数(x)将返回0,如果为奇数,则返回x[0]

您还可以像这样改进firstOddCount:奇数(x)将始终返回相同的值,因此只计算一次

int i;
int y;
for (i = 0; i < x.length; i++) {
    y = x[i] % 2;
    if (y == 1) {
        return x[i];
    }
}
return 0;
公共静态int firstOddCount(int x[]{
int first奇数=奇数(x);
int c=0;
对于(int i=0;i

}

您的特殊问题是,如果找到偶数,则返回
0
。这意味着列表
{2,4,6,8,1}
将给您
0
,而不是
1
,作为第一个奇数

您应该做的是忽略前导偶数并继续处理列表

然而,按照您组织程序的方式,您将处理列表中的第一个全偶数部分两次,一次在
odd()
中查找第一个奇数,然后再次在
firstOddCount()
中计算该数字的数量-这完全没有必要

一旦你找到了第一个奇数,我认为你可以合理地确定这个数字(或任何其他奇数)在你已经搜索过的空间中不存在。否则它将是第一个奇数。因此,回过头来再次查看列表的最初部分是没有意义的

您可以轻松地只处理列表一次的方法如下:

public static int firstOddCount(int x[]) {
   int firstOdd = odd(x);
   int c=0;
   for(int i=0; i < x.length; i++) {
       if (x[i]==firstOdd)
            c++;
   }
   return c;
public static int firstOddCount(int number[]){
//查找列表的第一个奇数或结尾。
int idx=0,len=numbers.length;
而((idxcount(第一个(奇数))是0。
if(idx==len)
返回0;
//否则,从当前位置开始计数。
int count=1,oddnum=number[idx];
而(++idx
您希望得到什么样的输出?您将得到什么?
public static int firstOddCount(int x[]) {
   int firstOdd = odd(x);
   int c=0;
   for(int i=0; i < x.length; i++) {
       if (x[i]==firstOdd)
            c++;
   }
   return c;
public static int firstOddCount (int numbers[]) {
    // Find first odd number or end of list.

    int idx = 0, len = numbers.length;
    while ((idx < len) && ((numbers[idx] % 2) == 0)
        idx++;

    // If at end of list, everything is even => count(first(odd)) is 0.

    if (idx == len)
        return 0;

    // Otherwise, start counting from current position.

    int count = 1, oddnum = numbers[idx];
    while (++idx < len)
        if (numbers[idx] == oddnum)
            count++;

    return count;
}