添加计数值(用于循环)
本文关键字:用于 循环 添加 | 更新日期: 2023-09-27 18:12:28
我有这个问题,我应该写代码并检查数组中数字(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
是不是你打错了。。。。
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;
}
}
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