其他声明不打印。使用二进制搜索

时间:2015-04-28 02:48:34

标签: java if-statement binary-search

我有一个数字列表,我试图在此列表中找到数字“58”和“85”。如果是数字,则应打印True,否则打印为False。到目前为止,我一直试图获得58个部分。当我运行程序时,它打印的全部是“True 58” 我的其他陈述有什么问题吗?我似乎无法找到问题。

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行。每一行都返回“真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

3 个答案:

答案 0 :(得分:3)

一个=是赋值(然后您测试指定为副作用的值)。你需要两个==喜欢

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]);
}

答案 1 :(得分:0)

您在测试中使用单个等号,这意味着您要分配值然后测试它们。

您的代码与此相同:

    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]);

由于找到的只能是truefalse,您可以将其缩短为:

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

答案 2 :(得分:0)

您正在使用=分配运营商,这意味着

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