2016-03-02 20 views
0

我得到的任务是总结除了以6开头的部分之外的数组的所有值,直到出现下一个7。 7之后的值应该再次加到我的总和中。Java - 总结除特定部分之外的Array的值

这里是我的解决方案之一:

 if(nums == null){ 
      return 0; 
     }else if(nums.length == 0){ 
      return 0; 
     }else{ 
      int sum = 0; 
      int countTill7 = 0; 
      boolean six = false; 

      for(int i = 0; i < nums.length; i++){ 
       if(nums[i] != 6){ 
        sum += nums[i]; 
       }else if(nums[i] == 6){ 
        six = true; 
        countTill7++; 
       }else if(six == true && nums[i - 1] == 7){ 
        six = false; 
        sum += nums[i]; 
       } 

      } 

      return sum; 
     } 

我无法找到问题..

+3

你张贴的问题,一个解决方案。我们能帮你什么吗? – Maroun

+1

他发布的解决方案是错误的 – ManKeer

+0

什么是以6开头的部分?每个以'6'开始的数字,像是'64'还是以'6'开始的索引? – SomeJavaGuy

回答

0

以下是我SUMED了一个数组的所有值,除了有6开始,直到节接下来的7出现

package codingbat.array3; 

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

    /** 
    * Return the sum of the numbers in the array, 
    * except ignore sections of numbers starting with a 6 and 
    * extending to the next 7 
    * (every 6 will be followed by at least one 7). 
    * Return 0 for no numbers. 
    * 
    * sum67({1, 2, 2}) → 5 
    * sum67({1, 2, 2, 6, 99, 99, 7}) → 5 
    * sum67({1, 1, 6, 7, 2}) → 4 
    */ 
    public int sum67(int[] nums) 
    { 
     int sum = 0; 
     boolean got6 = false; 

     for (int i = 0; i < nums.length; i++) 
     { 
      if (6 == nums[i]) 
      { 
       got6 = true; 
      } 
      else if (got6 && 7 == nums[i]) 
      { 
       got6 = false; 
      } 
      else if (!got6) 
      { 
       sum += nums[i];   
      } 
     } 

     return sum; 
    } 
} 
+0

看来,OP至少包括7'否则如果six == true && nums [i - 1] == 7){ six = false; sum + = nums [i]; } ' – Neijwiert

+1

@Neijwiert ty评论。我重新阅读OP多次询问的内容。现在你做同样的:)。 OP在7 ** _should_ again _be added_之后询问“**值”。 – Willmore

+0

是的,这是有点奇怪OP的方式问它嘿。我认为这可能意味着任何一种方式。但是,如果只有第一个范围应该跳过,那么它就可以工作。但我误导了OP有nums [I - 1] == 7,然后做了总和加法。 – Neijwiert

-1

你可以做同样的东西,

boolean flag = true, oneTimeDone = false; 

    for(int i = 0; i < nums.length; i++){ 
     if(flag){ 
     if(nums[i] == 6 && !oneTimeDone){ 
        flag = false; 
     }else{ 
      sum += nums[i]; 
     } 
    }else{ 
      if(nums[i] == 7){ 
        oneTimeDone = true; 
        flag = true; 
      } 

    } 
    } 

更新Out-地说:

enter image description here

+0

@Neijwiert恳求输出,'oneTimeDone'变量做了很多...认为如果6 - 传入7后,那么它应该算。 –

+0

我道歉,我出于某种原因认为OP要6-7的范围,并排除其他一切。尽管如此,我并没有失望。 – Neijwiert

+0

不需要使用oneTimeDone变量 – ManKeer

0

我觉得你只是错过一个布尔

反过来

if(nums[i] != 6) 

if(nums[i] != 6 && !six) 
-1
 int sum = 0; 
     int countTill7 = 0; 
     boolean six = false, seven=false; 

     for(int i = 0; i < nums.length; i++){ 
      if(six==false && seven==false){ 
       if(nums[i]==6) six=true; 
       else sum += nums[i]; 
      }else if(six==false || seven==false) { 
       if(nums[i]==7) seven=true; 
      }else { 
       sum += nums[i]; 
      } 
     } 

     return sum; 
+0

同样,没有必要使用两个标志,那么更多的解决方案已经足够了 – ManKeer

1

没有必要使用状态变量来表示我们是否在“6-7”块:

int sum = 0; 
int i = 0; 
while (i < nums.length) { 
    // Sum up the numbers until we find a 6. 
    while (i < nums.length && nums[i] != 6) { 
    sum += nums[i]; 
    ++i; 
    } 
    if (i < nums.length) { 
    // The i-th number is a 6. 
    // Increase i until the (i-1)-th number is a 7, 
    // since then i points to the next number we 
    // should add from. 
    do { 
     ++i; 
    } while (i <= nums.length && nums[i - 1] != 7); 
    } 
} 
+0

为什么要用3个循环来做到这一点? – Neijwiert