简体   繁体   English

我似乎在代码中找不到任何逻辑错误,但仍无法按预期工作

[英]I can't seem to find any logic errors in the code, but it still doesn't work as expected

Given an array of ints, return true if the value 3 appears in the array exactly 3 times, and no 3's are next to each other. 给定一个整数数组,如果值3恰好出现在数组中3次且没有3个相邻,则返回true。

I'm just a beginner programmer having some trouble at a codingbat lesson. 我只是一个初学者,在编码技巧课程上遇到了一些麻烦。 The logic seems fine. 逻辑似乎很好。 I've explained to the "rubber duck" a thousand times and I found no problem with it. 我已经向“橡皮鸭”解释了1000次,但我发现它没有问题。 All codingbat tests run as expected, except for the "other tests" tabs, which I cannot see the specific numbers in the array and cannot compare with the code. 除“其他测试”选项卡外,所有的codingbat测试均按预期运行,我无法看到数组中的特定数字,也无法与代码进行比较。 I'm really puzzled by this one, hope you can help me! 我对此感到非常困惑,希望您能帮助我!

public boolean haveThree(int[] a) {

    int count = 0;           //to count the appearences of 3
    boolean doLado = false;   //to check if a 3 is next to another 3

    if(a[0] == 3)    // check if first index is 3
        count++;      // add one if it is

    for(int i=1; i<a.length ; i++) { //loop starting at 1 to check rest of array

        if(a[i] == 3) {     // check if i is 3
            if(a[i-1] == a[i]) // if i its 3, check if the previous index was also 3
                return false;   // if it was indeed {..,3,3,..} return false
            else
                count++;        // else add 1 to the counter
        }
    }

    if(count == 3) //if counter of 3s equals 3 return true
        return true;

    return false; //else return false
}


tests                                  Expected  Run        
haveThree([3, 1, 3, 1, 3])----------- → true    true    OK  
haveThree([3, 1, 3, 3])---------------→ false   false   OK  
haveThree([3, 4, 3, 3, 4])------------→ false   false   OK  
haveThree([1, 3, 1, 3, 1, 2])---------→ false   false   OK  
haveThree([1, 3, 1, 3, 1, 3])---------→ true    true    OK  
haveThree([1, 3, 3, 1, 3])------------→ false   false   OK  
haveThree([1, 3, 1, 3, 1, 3, 4, 3])---→ false   false   OK  
haveThree([3, 4, 3, 4, 3, 4, 4])----- → true    true    OK  
haveThree([3, 3, 3])------------------→ false   false   OK  
haveThree([1, 3])---------------------→ false   false   OK  
haveThree([3])------------------------→ false   false   OK  
haveThree([1])------------------------→ false   false   OK  

other tests-----------------------------X

You don't handle null and you don't use doLado ; 您不会处理null ,也不会使用doLado also you don't need an if at the end to test for count == 3 . 还你不需要的if在年底测试count == 3 I would simplify it to something like 我将其简化为

public boolean haveThree(int[] a) {
    if (a == null || a.length < 3) {
        return false;
    }
    int count = 0;
    for (int i = 0; i < a.length; i++) {
        if (a[i] == 3) {
            if (i > 0 && a[i - 1] == 3) {
                return false;
            }
            count++;
        }
    }
    return count == 3;
}

null check is missing in your code to check if array a is null. 代码中缺少空检查以检查数组a是否为空。

You code will work fine if you add null check alone. 如果单独添加null检查,则代码将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM