Моя java программа не имеет ошибок, однако, когда я запускаю ее и ввожу номер для поиска, он никогда не находит никакого числа. Любое число, указанное в выводе, говорит: «Значение не найдено». Это сборы в четверг, и я не могу понять, что не так.
/ * Джонатан Поло Java Программирование CSC_214 02-27-2020 Профессор Алямал Напишите программу при последовательном и двоичном поиске. * /
import java.util.Scanner;
public class SeqBinSearches
{
public static void main(String[]args)
{
Scanner jp = new Scanner(System.in);
final int SIZE = 20;
int option = 0, key = 0;
seq_search();
bin_search();
do{
System.out.println("Menu of conversions:\n" +
"1 = Sequential search array.\n" +
"2 = Binary search array.\n" +
"3 = Ext\n");
option = jp.nextInt();
switch(option)
{
case 1:{
System.out.print("Sequential Search\n");
System.out.print("Enter number to find: \n");
key = jp.nextInt();
seq_search();
}
break;
case 2:{
System.out.print("Binary Search\n");
System.out.print("Enter number to find: \n");
bin_search();
}
break;
default:
System.out.print("Wrong Entry\n");
}
System.out.printf("Enter 1 if you like to run program again\n"+
"Otherwise enter any other number to end the program.");
option = jp.nextInt();
}while (option == 1);
}
public static void seq_search()
{
Scanner jp = new Scanner(System.in);
final int SIZE = 20;
int target = 0;
int []seg = {2, 6, 7, 12, 90, 60, 34, 56, 45, 32, 78, 88, 21, 30,
15, 29, 99, 67, 1, 50};
for(int i = 0; i <SIZE; i++)
{
if(target == seg[i])
{
System.out.printf("Value found: ", i + 1,"\n");
target =2;
}
else target =0;
}
if(target ==0)
{
System.out.print("Value was not found\n");
}
}
public static int bin_search()
{
Scanner jp = new Scanner(System.in);
final int SIZE = 20;
int low = 0, top = SIZE-1, found = 0, bottom = 0, key = 0;
int []bin= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
while(bottom <= top)
{
int midpoint = (top + bottom)/2;
if(bin[midpoint] == key)
{
return midpoint;
}
else if (key < bin[midpoint])
{
top = midpoint -1;
}
else
{
bottom = midpoint + 1;
}
}
return -1;
}
}