我有这个问题,我应该编写代码并检查数组中数字的可分性,数字(goodValue和badValue)..如果好的和坏的值可以被数组中的数字整除,我们添加+2,如果数字可以被良好的值整除,我们加+1,如果该值只能被badValue整除,我们减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
答案 0 :(得分:0)
是你输错了......
score=+2;
应该是
score += 2;
与其他语句类似,它应该是
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;
}
}
答案 1 :(得分: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