else语句未打印使用二进制搜索

llew8vvj  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(381)

我有一个数字列表,我正试图找到这个列表中的数字“58”和“85”。如果是数字,则打印true,否则打印false。到目前为止,我一直在努力完成58部分。当我运行程序时,它显示的都是“true 58”我的else语句有什么问题吗?我好像找不到问题。

import java.util.Arrays;

public class asgn9
{
    public static void main(String[] args)
    {
        int[] data = { 12, 25, 35, 45, 58, 64, 77, 80, 84, 93 };
        int searchedValue = 58;
        int searchedValue2 = 85;

        keyTest(data, searchedValue, searchedValue2);
    }

    public static void keyTest(int[] data, int searchedValue, int searchedValue2)
    {
        boolean found = false; 
        int low = 0;
        int high = data.length - 1;
        int pos = 0;

        while (low <= high && !found)
        {
            pos = (low + high) / 2; // Midpoint of the subsequence
            if (data[pos] == searchedValue) 
            { found = true; }    
            if (data[pos] < searchedValue) 
            { low = pos + 1; }      // Look in first half
            else
            { high = pos - 1; }     // Look in second half
        }
        if (found = false)
            System.out.println("False " + data[pos]);
        else if (found = true)
            System.out.println("True " + data[pos]);
    }//end of keyTest
}

编辑:
我用了for循环,现在我得到了10行我应该。每一行都返回“true 58”。我也尝试过在实际搜索中编辑语句,但是我不确定它们是否必要。

for (i = 0; i < data.length; i++)
    {

    while (low <= high && !found)
    {
      pos = (low + high) / 2;  // Midpoint of the subsequence
      if (data[pos] == searchedValue) 
        { found = true; }    
         else { found = false;}
        { low = pos + 1; }     // Look in first half
         if (data[pos] == searchedValue)
         {found = true;}
         else { high = pos - 1; }// Look in second half
         if (data[pos] == searchedValue)
         {found = true;}
         else { found = false;}

    }

    if (!found)
    System.out.println("False " + data[pos]);
    else
    System.out.println("True " + data[pos]);

    }//end of for
tgabmvqs

tgabmvqs1#

1 = 是赋值(然后测试赋值的副作用)。你需要两个 == 喜欢

if (found == false) {
    System.out.println("False " + data[pos]);
} else if (found == true) {
    System.out.println("True " + data[pos]);
}

另外,我更喜欢较短的布尔值 ! 喜欢

if (!found) {
    System.out.println("False " + data[pos]);
} else {
    System.out.println("True " + data[pos]);
}
7tofc5zh

7tofc5zh2#

您正在使用 = 这是赋值运算符,意思是

found = false will assign the value `false` to found so it is not going to `false`

你应该使用 == 进行状况检查。
自从你的 found 旗子是 boolean ,您应该尝试以下代码

if (!found)
            System.out.println("False " + data[pos]);
 else if (found)
            System.out.println("True " + data[pos]);
    }//end of keyTes
wljmcqd8

wljmcqd83#

你在测试中使用单等号,这意味着你在赋值然后测试它们。
您的代码等于:

if (false)
        System.out.println("False " + data[pos]);
    else if (true)
        System.out.println("True " + data[pos]);

使用双等号进行比较:

if (found == false)
        System.out.println("False " + data[pos]);
    else if (found == true)
        System.out.println("True " + data[pos]);

既然找到的只能是 true 或者 false 您可以将其缩短为:

if (found == false)
        System.out.println("False " + data[pos]);
    else
        System.out.println("True " + data[pos]);

相关问题