Calculating the sum of all odd array indexes

后端 未结 5 948
予麋鹿
予麋鹿 2021-01-24 19:00

I want to calculate the sum of all odd array indexes, but I\'m having some trouble finding the right way to do it.

Here\'s my code so far:

    String id         


        
5条回答
  •  礼貌的吻别
    2021-01-24 19:31

    Just edited your code:

    String id = "9506265088085";
    int[] intArray = new int[id.length()];
    int sum = 0;
    
    for (int i = 0; i < intArray.length; i++) {
    
        if (i%2!=0)
        {
            sum += Integer.parseInt(String.valueOf(id.charAt(i));
    
        }} 
    
        System.out.println(sum);
    

提交回复
热议问题