0

我有这个问题,我应该编写代码并检查数组中数字的可分性,用数字(goodValue 和 badValue).. 如果好的和坏的值可以被数组中的数字整除,我们添加 +2 ,如果这个数能被好值整除,我们加1,如果这个值只能被坏值整除,我们减1。

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
4

2 回答 2

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
于 2013-05-08T05:44:19.370 回答
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;
    }
}
于 2013-05-08T04:15:40.320 回答