简体   繁体   English

顶点数组结果未按预期显示

[英]apex array result is not showing as expected

I'm writing apex (salesforce) code to count total even and odd numbers in an array.我正在编写顶点(salesforce)代码来计算数组中的偶数和奇数总数。 Instead of counting numbers in the array, it is showing me numbers from array size and evaluates them.它不是计算数组中的数字,而是向我显示数组大小的数字并评估它们。

public class even_odd_11{ 
public void check_number() 
{
    integer i, odd = 0, even = 0; 
    integer[] arr = new integer[]{2,41,51,61}; 
        for (i=0; i<arr.size(); i++) 
    {
            if(Math.mod(arr[i], 2) == 0)
        {
            even++;
            system.debug('even numbers -- ' +i);
        }
        
    else
    {
        odd++;
        system.debug('odd numbers -- ' +i);
    }
            
    }
    system.debug('Total even numbers are ' +even);
    system.debug('Total odd numbers are ' +odd);
    system.debug(' size of array -- ' +arr.size());
    
}

} }

According to the code, I need to count total even and odd numbers and display those numbers as mentioned in array.根据代码,我需要计算总的偶数和奇数并显示数组中提到的这些数字。 For eg例如

total even number are 1 even numbers -- 2总偶数是 1 个偶数 -- 2

total odd numbers are- 3 odd numbers -- 41,51,61总奇数是 - 3个奇数 - 41,51,61

Please help me with the code, where I'm doing wrong.请帮我写代码,我做错了。

结果显示

Thank You谢谢你

I see 3 problems:我看到 3 个问题:

  1. The if is wrong. if是错误的。 You check if the loop counter is odd, even, odd, even... What you need is if(Math.mod(arr[i], 2) == 0) .您检查循环计数器是否为奇数、偶数、奇数、偶数……您需要的是if(Math.mod(arr[i], 2) == 0) Use the value at that index, not just the index itself.使用该索引处的值,而不仅仅是索引本身。

  2. The first two system.debugs - I think you intended them to display the odd / even variable, not the loop counter i .前两个 system.debugs - 我认为您希望它们显示odd / even变量,而不是循环计数器i Probably copy-paste error?可能是复制粘贴错误?

    System.debug('even numbers -- ' + even); should work better?应该工作得更好?

  3. Not terribly evil but weird - in bottom system.debugs you want to just display even , not even++ .不是非常邪恶但很奇怪 - 在底部 system.debugs 你只想显示even ,而不是even++ As it's end of the function it doesn't matter much but if you'd be returning this value and using it further in the program - subtle, nasty error.由于它是 function 的结尾,它并不重要,但如果你要返回这个值并在程序中进一步使用它 - 微妙的,讨厌的错误。

@eyescream, I have edited my code as you suggested. @eyescream,我已经按照你的建议编辑了我的代码。 In line排队

system.debug('even numbers -- ' +arr[i]);

I have used arr[i] to show which value is odd / even我用 arr[i] 来显示哪个值是奇数/偶数

public class even_odd_11{ 
public void check_number() 
{
    integer i, odd = 0, even = 0; 
    integer[] arr = new integer[]{2,41,51,61}; 
        for (i=0; i<arr.size(); i++) 
    {
            if(Math.mod(arr[i], 2) == 0)
        {
            even++;
            system.debug('even numbers -- ' +arr[i]);
        }
        
    else
    {
        odd++;
        system.debug('odd numbers -- ' +arr[i]);
    }
            
    }
    system.debug('Total even numbers are ' +even);
    system.debug('Total odd numbers are ' +odd);
    system.debug(' size of array -- ' +arr.size());
    
}

} }

Now my code is working fine.现在我的代码工作正常。 Thanks谢谢

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

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