简体   繁体   English

Java代码中的编译错误(找不到符号)

[英]Compilation error in Java code (cannot find symbol)

I've changed my code quite a bit. 我已经更改了很多代码。 I apologize for not being more clear before. 对于以前不清楚的事情,我深表歉意。 I need to find the sum of ints at the odd index positions within an array. 我需要找到数组中奇数索引位置的整数之和。 I've modified my code to this: 我已经将代码修改为:

  public class OddIndex 
   {
     public static void main(String[] args) 
      {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        OddIndex arr = new OddIndex();
        int sum = 0;

        for (int i = 0 ; i < numbers.length; i++) 
         {
           if ((i%2!=0))
            {
              sum += Integer.parseInt(String.valueOf(numbers.length));              
            }
         }

         System.out.println(sum);
       }
     }

The output should be 30, but I'm receiving 50. Not sure where the error exists within the code. 输出应该是30,但我收到50。不确定代码中的错误在哪里。

EDIT 编辑

I've fixed it myself after all. 毕竟我已经自己修好了。 See below for answer. 参见下面的答案。 I appreciate the input from everyone! 感谢大家的宝贵意见!

You don't have to use oddarray. 您不必使用奇数数组。 First of all you haven't even imported oddArray. 首先,您甚至都没有导入oddArray。 You cannot use it. 您不能使用它。 Your question is really confusing. 您的问题确实令人困惑。 Im assuming you want to know which elements in your array are odd and then their sum. 我假设您想知道数组中的哪些元素是奇数,然后是它们的总和。

    public class OddArrayClient
    {
     public static void main( String[] args)
     {

       int [] intEle = {25, 2, 6, 86, 2, 9};

       int sum=0;
       System.out.print( "The odd elements of the array are: " );


       for (int i = 0; i < intEle.length; i++)
       {

         if (intEle[i]%2 !=0)
         {
         System.out.print(intEle[i] + " ");
         sum=sum+intEle[i];        
         }
       }


      System.out.println();

      System.out.println("The product of all odd elements is: "+sum);  
 }

} }

问题是您尚未定义或导入OddArray

public class OddArrayClient
{
  public static void main( String [] args )
   {
    int [] sampleArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    int sum=0;


    for ( int i = 0; i < sampleArray.length; i++ )
    {
      if (i%2 != 0)
      {
        sum=sum+sampleArray[i];
      }
    }

    System.out.print( "The sum of odd elements of the array is : " +sum);
   }
}

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

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