[英]Else statement not printing. Used binary search
I have a list of numbers and I am trying to find the numbers "58" and "85" in this list. 我有一个数字列表,我试图在这个列表中找到数字“58”和“85”。 It should print True if it is the number, and False otherwise.
如果是数字,则应打印True,否则打印为False。 I have just been trying to get the 58 part so far.
到目前为止,我一直试图获得58个部分。 When I run the program, all it prints is "True 58" Is there something wrong with my else statement?
当我运行程序时,它打印的全部是“真58”我的else语句有问题吗? I can't seem to find the problem.
我似乎无法找到问题。
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
}
Edit: 编辑:
I used a for loop and now I am getting the 10 lines I should be. 我使用了for循环,现在我得到了10行。 Each line is returning "True 58."
每一行都返回“真58”。 I also tried editing statements in the actual search, however I'm not sure if they are necessary.
我也尝试在实际搜索中编辑语句,但是我不确定它们是否有必要。
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
One =
is assignment (and then you test the value assigned as a side-effect). 一个
=
是赋值(然后你测试指定为副作用的值)。 You need two ==
like 你需要两个
==
喜欢
if (found == false) {
System.out.println("False " + data[pos]);
} else if (found == true) {
System.out.println("True " + data[pos]);
}
Also, I'd prefer the shorter boolean not !
另外,我更喜欢较短的布尔值
!
like 喜欢
if (!found) {
System.out.println("False " + data[pos]);
} else {
System.out.println("True " + data[pos]);
}
You're using single equal signs in your tests which means that you're assigning values and then testing them. 您在测试中使用单个等号,这意味着您要分配值然后测试它们。
Your code is equal to this: 你的代码与此相同:
if (false)
System.out.println("False " + data[pos]);
else if (true)
System.out.println("True " + data[pos]);
Use double equal signs for comparison: 使用双等号进行比较:
if (found == false)
System.out.println("False " + data[pos]);
else if (found == true)
System.out.println("True " + data[pos]);
And since found can only be either true
or false
you can shorten it to: 由于发现只能是
true
或false
你可以缩短为:
if (found == false)
System.out.println("False " + data[pos]);
else
System.out.println("True " + data[pos]);
You are using =
which is assigning operator, which means 您正在使用
=
分配运算符,这意味着
found = false will assign the value `false` to found so it is not going to `false`
you should use ==
for condition check. 你应该使用
==
进行条件检查。
Since your found
flag is boolean
, you should try the following code 由于您
found
标志是boolean
,您应该尝试以下代码
if (!found)
System.out.println("False " + data[pos]);
else if (found)
System.out.println("True " + data[pos]);
}//end of keyTes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.