2013-05-08 103 views
0

我有這個問題,我應該編寫代碼並檢查數組中數字的可分性,數字(goodValue和badValue)..如果好的和壞的值可以被整除陣列中的數量,我們增加+2,如果該號碼是物有所值整除,我們添加+1,如果該值是整除只有我們的badValue減去1添加計數值(for循環)

public class Array { 
    static int[] ArrayA ={8, 4, 3, 12, 7, 9}; 
    static int[] ArrayB ={3, 8, 14, 12, 10, 16, 6}; 
    static int score; 

public static void main(String [] args){ 

    score = scoreArray(ArrayA, 2, 3); 
    System.out.println("First score for arrayA: " + score); 
    score = scoreArray(ArrayA, 3, 4); 
    System.out.println("Second score for ArrayA: " +score); 
    score = scoreArray(ArrayB, 5, 2); 
    System.out.println("First score for ArrayB: " +score); 
    score = scoreArray(ArrayB, 3, 7); 
    System.out.println("Second score for ArrayB: " +score); 


} 

private static int scoreArray(int [] theArray, int goodValue, int badValue){ 
    for (int i=0; i<=theArray.length; i++){ 
     if((i%goodValue & i%badValue)==0){ 
      score=+2;    
     } 
     else if (i%goodValue==0){ 
      score=+1; 
     } 
     else if((i%badValue==0)){ 
      score= -1; 
     } 
     score+=score; 
    } 

    return score; 

    } 
} 

我應該得到這

Firts score for arrayA: 1 
Second score for arrayA: 3 
First sccore for arrayB: -3 
Second score for ArrayB: 2 

和我得到這個

First score for arrayA: 2 
Second score for ArrayA: 2 
First score for ArrayB: 2 
Second score for ArrayB: 2 

回答

0

是不是你的錯類型....

score=+2; 

應該

score += 2; 

同樣在其他else語句應該是

score += 1; 

score -=1; 

我想我並不完全理解你在問題中提到的措辭。你在一個地方使用數字,並在另一個地方使用數字,這讓我感到困惑。

我提高了分值的計算邏輯,但沒有給予確切的答案你期待

private static int scoreArray(int [] theArray, int goodValue, int badValue){ 
      score = 0; 
    for (int i=0; i<theArray.length; i++){ 
     if(((theArray[i]%goodValue)==0) && ((theArray[i]%badValue)==0)){ 
      score += 2;    
     } 
     else if (theArray[i]%goodValue==0){ 
      score+=1; 
     } 
     else if((theArray[i]%badValue==0)){ 
      score -= 1; 
     } 
    } 
    return score; 
    } 
} 
+0

它仍然沒有給我正確的答案。我無法弄清楚什麼是錯的。它是for循環還是任何語句? – Guille 2013-05-08 04:20:53

+0

我已經更新了答案......但我對問題的措辭感到困惑 - 如果數字可以被良好的值整除,我們加1,如果值只能被badValue整除,我們減1。 - 您在上次聲明中的含義是什麼。 – Guanxi 2013-05-08 05:20:35

+0

@Guille我已經更新了答案。現在問題更簡單了。你是通過價值或壞價值來劃分「我」,相反,你應該對我們的數組[i]。另外你期望的答案是不正確的。它應該是2,2,-3,2 – Guanxi 2013-05-08 14:58:31

0
public class Array 
{ 
static int[] ArrayA ={8, 4, 3, 12, 7, 9}; 
static int[] ArrayB ={3, 8, 14, 12, 10, 16, 6}; 
static int score=0; 

public static void main(String [] args) 
{ 

score = scoreArray(ArrayA, 2, 3); 
System.out.println("First score for arrayA: " + score); 
score = scoreArray(ArrayA, 3, 4); 
System.out.println("Second score for ArrayA: " +score); 
score = scoreArray(ArrayB, 5, 2); 
System.out.println("First score for ArrayB: " +score); 
score = scoreArray(ArrayB, 3, 7); 
System.out.println("Second score for ArrayB: " +score); 


} 

public static int scoreArray(int [] theArray, int goodValue, int badValue) 
{ score =0; 
for (int i=0; i<theArray.length; i++) 
{ 

if(((theArray[i]%goodValue)==0) && ((theArray[i]%badValue)==0)) 
    { 

     score = score+2;   

    } 
    else if (theArray[i]%goodValue==0) 
    { 
     score = score+1; 
    } 
    else if((theArray[i]%badValue==0)) 
    { 
     score = score+(-1); 
    } 



} 
return score; 
} 
    } 

它會正常工作按您的要求......如果任何問題,發表您的意見。

output 
    First score for arrayA: 2 
    Second score for ArrayA: 2 
    First score for ArrayB: -3 
    Second score for ArrayB: 2