简体   繁体   English

带循环的布尔值,用于检查数组

[英]Boolean with loops to check an array

Write Java code for a loop that sets boolean variable isOrdered to true if the elements of a given array of ints called are in non-decreasing order, otherwise it sets isOrdered to false. 如果给定的int数组的元素的顺序为非降序,请为将布尔变量isOrdered为true的循环编写Java代码,否则将isOrdered设置为false。

int i[] = { 1, 2, 3, 4, 5 };
int b = 0;
boolean isOrdered = false;
while (b < i.length) {
    if (i[0] <= i[b]) {
        isOrdered = true;
    }
    b++;
}
out.println(isOrdered);

Am I doing this correctly? 我这样做正确吗?

Let's see what is wrong. 让我们看看有什么问题。

if (i[0] <= i[b])

This is the main area which troubles me with your code. 这是困扰您代码的主要方面。 How are you checking if the next value you is lower/higher then i[b] you are only comparing values at index zero to index b! 您如何检查下一个值是否比i[b]高/低,您仅将索引0的值与索引b的值进行比较!

Essentially your code would look like this in a loop. 本质上,您的代码看起来像是在循环中。

/w int i[] = { 1, 2, 3, 4, 5 }; / w int i[] = { 1, 2, 3, 4, 5 };

i[0] i[b]
1     1
1     2
1     3
...

you get the picture right? 你看对图片吗? What you really need is to check the next value after b. 您真正需要的是检查b.之后的下一个值b.

so the code would look like i[b] > i[b+1] 因此代码看起来像i[b] > i[b+1]

Honestly, you could probably make it work on how you initialized the order of isOrdered to true and false. 老实说,您可能可以使它对如何将isOrdered的顺序初始化为true和false isOrdered I would first initialize it to true. 我首先将其初始化为true。 Then the idea is to break out of whatever process you are doing if you find a fallacy in the question with a false. 然后的想法是,如果您在问题中发现一个带有false.的谬论,那就摆脱您正在做的任何事情false. Please look at my examples for further references . 请查看我的示例以获取更多参考

iterative 迭代

boolean isOrdered = true;

while(isOrdered && array.length - 1 > b){
     if(array[b] > array[b+1]) isOrdered = false;
     b++;
}

recursive 递归

boolean isOrdered(int[] array, index){
    if(index == array.length - 1) return true;
    if(array[index] > array[index + 1]) return false;
    return isOrdered(array, index + 1);
} 

The recursive method for this is waaaaaaaaaay cooler. 递归方法是waaaaaaaaaay cooler。

No; 没有; you are only checking whether the first element of the array is smaller than or equal to at least one of the elements - including the first element itself, which will always be equal to itself, setting isOrdered to true no matter what the remaining elements are. 您只是在检查数组的第一个元素是否小于或等于至少一个元素-包括第一个元素本身(将始终等于其自身),无论其余元素是什么,都将isOrdered设置为true

Hint #1: you should be comparing every element except the first one with the element immediately before it. 提示#1:您应该将除第一个元素之外的每个元素与紧接其前的元素进行比较。

Hint #2: you should be optimistic and assume that the array is ordered, then search for a counter-example. 提示2:您应该乐观,并假设数组是有序的,然后搜索一个反例。 As soon as you found a pair of elements that violate the ordering, set isOrdered to false and break out of the loop. 一旦发现一对违反顺序的元素,请将isOrdered设置为false并退出循环。

do a for loop on the size of the array to check if the next item is less then the current: 对数组的大小执行for循环,以检查下一项是否小于当前项:

   int arr[] = { 1, 2, 3, 4, 5 };
    int b = 0;
    boolean isOrdered = true;
    for (int i = 0; i< arr.length - 1 ; i ++) {
        if (arr[i] > arr[i+1])
        {
            isOrdered = false;
            break;
        }
        b++;
    }
    out.println(isOrdered);

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

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