【问题标题】:Sum all odd indexes in an array对数组中的所有奇数索引求和
【发布时间】:2014-11-11 21:17:50
【问题描述】:

我必须按照这些说明完成一个程序:

public class SumOddIndex {
    public static void main(String[] args){
       int[] array = {1,1,8,4,2,6};
       // Call the method sum, receive an integer value, and print the value on output. 
       // A call passing the example array should result in a value of 11. 
       // Use a while loop.
   }

这里编写了一个叫做 sum 的方法。方法 sum 应该以一个 int - 数组作为参数,并返回所有具有奇数索引号的数组单元格中所有值的总和。 使用 WHILE- 循环! }

这是我迄今为止尝试过的,但它不起作用。

public static void main(String[] args) {
        int[] array = {1, 1, 8, 4, 2, 6};
        sum(array);
}

public static int sum(int[] y){
   int sum = 0;
   int i = 0;
   while(y.len

   public static int sum(int[] y) {
      int sum = 0;
      int i = 0;
      while (y.length%2 == 1) {
         //y.length%2 == 1;
         sum += y[i];
         i++;
      }
      System.out.println(y[i]);
      return i;
   }
}

【问题讨论】:

  • y.length%2 - 告诉我这是什么。
  • 我想我可以计算出我数组的所有奇数元素,但我认为我错了:(

标签: java arrays


【解决方案1】:

假设您使用的是 java,请尝试以下操作:

int i = 0;
int sum = 0;
while(i < myArray.length){
   sum += myArray[i];
   i++;
}

这是一个 while 循环,仅当您的计数器变量小于数组的大小时才会执行(这样您就永远不会有超出范围的索引)。在循环内部,它将把计数器索引处的值加到总和中。

不要忘记在 while 循环中增加你的计数器,否则你会被困在一个无限循环中。

编辑

我误读了你的问题,但如果你只想对奇数值求和,试试这个:

while(i < myArray.length){
   if(myArray[i] % 2 == 1){ // If value is odd
      sum += myArray[i];
   }
   i++;
}

编辑 2

要仅对奇数索引求和,请更改上面的代码以检查您的计数器是否为奇数,而不是该索引处的值,如下所示:

while(i < myArray.length){
   if(i % 2 == 1){ // If index is odd
      sum += myArray[i];
   }
   i++;
}

或者,您可以从 1 开始索引并每次增加 2。我只会给这个伪代码:

// Start counter at 1
// While counter is less than array length
// Add element's value to the sum
// Increment counter by 2

【讨论】:

  • hmm 不,它不起作用,所以我需要一个 while 循环,巫婆对 1 3 和 5 的元素求和。所以结果是 11
  • 这绝对不是最佳解决方案...相反,i 从 1 开始并以 2 为增量是理想的
  • @Saboteur 你确定要加到总和中,而不仅仅是设置总和等于值吗?
  • 不,我不确定:/ 我现在尝试解决这个问题 2 小时:/ public static int sum 是我创建的。我不知道它是否正确......这是因为我在这里;)
  • 您的代码中有sum = myArray[i]sum += myArray[i] 吗?
【解决方案2】:

在 Java 8 中使用 Streams,您可以使用 IntStreamfilterreduce 到总和:

int[] array = {1,1,8,4,2,6};
int sum = IntStream.range(0, array.length).filter(i -> i % 2 == 1).map(i -> array[i]).sum();

【讨论】:

  • 这是旧的但不正确,.filter 只是添加索引,而不是值。
  • @awm 感谢您指出这一点,我相信更新应该包含一个有效的解决方案。
【解决方案3】:

我知道这个问题已经回答了,但我认为它仍然会有所帮助。

int i = 1;
int sum = 0;

while(i < myArray.length)
{            
    sum += myArray[i];  
    i += 2;
}

return sum;

我添加了@Logan Murphy 的优化建议。

【讨论】:

  • 这行得通,但是如果你从 1 开始并且每次递增 2,为什么要检查它是否是奇数?除非出现严重错误,i 现在总是很奇怪。
  • 糟糕,我忘了把它拿出来。
【解决方案4】:

使用流的过滤器和归约操作。

int[] 数组 = {1,1,8,4,2,6}; int sum = array.stream().filter(s -> s % 2 != 0).reduce(0, Integer::sum);

【讨论】:

    【解决方案5】:

    一种稍微优化的方法可以是计算一次数组的长度并将其存储在一个变量中,而不是在每次迭代中计算它。

    public int oddIndSum(int [] myArray){
        int i = 1;
        int sum = 0;
        int length = myArray.length;
        while(i < length){
            if(i % 2 == 1){ // If index is odd then add
            sum += myArray[i];
            }
            i++;
        }
        return sum;
    }
    

    【讨论】:

    • 嗨 @MohammadWael - 为什么不将您的贡献添加到 2014 年 11 月的原始接受答案中?也许考虑使用final int length
    猜你喜欢
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多