简体   繁体   中英

Sequential and Binary Java Program with Switch Case and Arrays. Output not correct. Program has no errors

My java program has no errors however when I run it and enter the number for it to find, never finds any number. Any number I put in the output says "Value not found." This is dues on Thursday and I can not figure out what is wrong.

/*Jonathan Polo Java Programming CSC_214 02-27-2020 Professor Aljamal Write a program if sequential and binary searches. */

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

In neither seq_search nor bin_search you haven't used the variable jp to get the input and search based on it.

That's why your program doesn't care about the inputs :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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