2016-03-02 22 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