验证2D数组的元素

huangapple go评论67阅读模式
英文:

Validating elements of a 2D array

问题

我的代码用于确保元素不超过100或小于0:

boolean test = true;
for (int i = 0; i < inArray.length; i++) {
    for (int j = 0; j < inArray[0].length; j++) {
        if (inArray[i][j] <= 0 || inArray[i][j] >= 100) {
            test = false;
        }
    }
}
return test;

但无论输入是什么,答案始终是true

我的输入array

{12, 1331, 23, 5, 1, 3},
{22, 23, 231, 21312, 1, 3},
{23, 31, 343, 3432, 1, 3},
{42, 12, 5454, 1212, 3, 9}

如果我输入这个,test应该等于false吧?

学院规定我除了在子模块末尾之外不能使用break;return

英文:

My code for making sure an element isn't over 100 or less than 0 is:

boolean test = true;
for (int i = 0; i &lt; inArray.length; i++) {
    for (int j = 0; j &lt; inArray[0].length; j++) {
        if (inArray[i][j] &lt;= 1 &amp;&amp; inArray[i][j] &gt;= 100) {
            test = false;
        }
    }
}
return test;

Except no matter what the input is the answer is always true

My input array is

{12, 1331, 23, 5, 1, 3},
{22, 23, 231, 21312, 1, 3},
{23, 31, 343, 3432, 1, 3},
{42, 12, 5454, 1212, 3, 9}

test should = false if I input this no?

College rules mean I cannot use break; or return anywhere other than the end of the sub module.

答案1

得分: 1

更新你的 for 循环条件为 i < inArray.length && testj < inArray[0].length && test,这会在找到无效数字后终止循环,并且更新你的 if 条件为 inArray[i][j] <= 1 || inArray[i][j] >= 100

boolean test = true;

for(int i = 0; i < inArray.length && test; i++){
    for(int j = 0; j < inArray[0].length && test; j++){ 
        if(inArray[i][j] <= 1 ||  inArray[i][j] >= 100){    
            test = false;
        }
    }
}

return test;

你也可以通过以下方式跳过一个 if 块:

boolean test = true;

for(int i = 0; i < inArray.length && test; i++){
    for(int j = 0; j < inArray[0].length && test; j++){ 
        test = (1 < inArray[i][j] && inArray[i][j] < 100);
    }
}

return test;
英文:

Update your for loop condition to i &lt; inArray.length &amp;&amp; test and j &lt; inArray[0].length &amp;&amp; test which would force your loop to end once an invalid nos is found
and your if condition to inArray[i][j] &lt;= 1 || inArray[i][j] &gt;= 100

boolean test = true

        for(int i = 0; i &lt; inArray.length &amp;&amp; test; i++){
            for(int j = 0; j &lt; inArray[0].length &amp;&amp; test; j++){ 
                if(inArray[i][j] &lt;= 1 ||  inArray[i][j] &gt;= 100){    
                    test = false;
                }
            }
        }

        return test;

you can also skip a if block by,

boolean test = true

        for(int i = 0; i &lt; inArray.length &amp;&amp; test; i++){
            for(int j = 0; j &lt; inArray[0].length &amp;&amp; test; j++){ 
                test = (1 &lt; inArray[i][j]  &amp;&amp; inArray[i][j] &lt; 100)  
            }
        }

        return test;

答案2

得分: 1

你面临三个问题,其中两个是关键的。首先,如果你想迭代遍历每个子数组,请测试其长度,而不是每次都测试数组[0]的长度。其次,整数不能小于2且不能大于99,因此你的条件语句 (inArray[i][j] <= 1 && inArray[i][j] >= 100) 永远不会成立。

然而,这里还有更多问题:如果你的测试纯粹是为了检查你的二维数组是否有效,在必要时不要做过多的工作。一旦找到一个不良元素,你可以立即停止。现在,我们可以通过在你的for循环条件中添加一个检查来实现。例如,我们可以将你的变量重命名为 valid(因为变量应该根据它们表示的内容进行命名),然后我们可以将外部循环更新为 for(int i=0; valid && i < arr.length; i++) { ... },内部循环也是如此,但我们不会这样做。这段代码"做了一件事情"(即为任何数组的数组测试"元素有效性"),"做一件事情"的代码应该在自己的方法中,方法名应描述其功能。这样做使得"不做任何不必要的工作"变得更加容易:我们在知道我们已经完成了足够的工作以产生答案后,就退出函数。

因此,让我们来做这个:

// 第一个修复:这段代码"做了某事"。将其作为一个方法来实现。
public boolean testForValidContent(int[][] arr) {
  // 我们不需要一个变量来跟踪事物:因为我们正在返回一个值,所以我们可以在返回时直接返回正确的值。

  for(int i = 0; i < arr.length; i++) {
    for(int j = 0; j < arr[i].length; j++) { // 第二个修复:检查正确数组的长度
      if(arr[i][j] <= 1 || arr[i][j] >= 100) { // 第三个修复:用或运算,而不是与运算

        // 在这一点上,我们知道数组无效,所以没有必要继续运行任何操作:
        return false;

      }
    }
  }

  // 我们已经检查了整个数组,一切都有效:
  return true;
}
英文:

You have three problems here, two of which are critical. First, if you want to iterate over each subarray, test its length, not the length of array[0] every time. And second, integers cannot be less than 2 and over 99, so your conditional (inArray[i][j] &lt;= 1 &amp;&amp; inArray[i][j] &gt;= 100) is never going to fire.

However, there's more at play here: if your test is purely to see whether your 2d array is valid, don't do more work than necessary. You can literally stop once you find a single bad element. Now, we could do that by adding a check to your for conditionals. E.g. we can rename your variable to valid (because you should name variables after what they represent), and then we can update your outer loop to for(int i=0; valid &amp;&amp; i &lt; arr.length; i++) { ... } with the corresponding valid &amp;&amp; ... in the inner loop, too, but we're not going to. This code "does a thing" (namely it "tests for element validity" for any array-of-arrays), and code that "does a thing" should be in its own method, with a method name that describes what it does. And this makes "not doing any of the work we don't need to do" much, much easier: we just exit the function when we know we've done enough work to produce an answer.

So let's do that:

// 1st fix: this code &quot;does soemthing&quot;. Do that as a method.
public boolean testForValidContent(int[][] arr) {
  // We don&#39;t need a variable to track things: because we&#39;re
  // returning a value, we can simply return the right value
  // at the moment we return.

  for(int i = 0; i &lt; arr.length; i++) {
    for(int j = 0; j &lt; arr[i].length; j++) { // 2nd fix: check the right array&#39;s length
      if(arri][j] &lt;= 1 || arr[i][j] &gt;= 100) { // 3th fix: OR, not AND

        // At this point we know the array is not valid, so there
        // is no point in running even a single thing more:
        return false;

      }
    }
  }

  // We checked the entire array, and everything&#39;s valid:
  return true;
}

huangapple
  • 本文由 发表于 2020年5月29日 22:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/62088407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定