java - How can I get the user input to use the proper instructions and quit out the program? -
so i'm having problem boolean method 'typeofsearch' user asked enter type of search use whether success, fail , or quit out search. method i'm using don't acknowledge success or fail input quit input causes recurse . doing wrong? advice!
import java.util.scanner; public class interpolation{ private static double[] arr; private static final int size = 1000; public static void main(string[] args) { scanner input = new scanner(system.in); arr = new double[size]; system.out.println("enter index: "); double sv = input.nextdouble(); int lb = 0; int ub = 999; randomfill(arr); system.out.println("sorted result:"); sort(); print(arr); typeofsearch(sv, lb, ub); system.out.println("interpolation result:"); system.out.println(interpolationsearch(arr, size, sv, lb, ub)); } public static int interpolationsearch(double[] arr, int size, double sv, int lb, int ub) { int result; int sp = (int) (lb + ((sv - arr[lb]) / (arr[ub] - arr[lb])) * (ub - lb)); if (arr[lb] == sv) { return lb; } else if (arr[sp] < sv && arr[sp + 1] > sv) { return -1; } if (arr[sp] < sv) { return sp; } else if (arr[sp] < sv) { result = interpolationsearch(arr, size, sv, (sp + 1), ub); } else { result = interpolationsearch(arr, size, sv, lb, sp - 1); } return result; } public static void print(double[] arr) { (double num : arr) { system.out.println(num + ""); } system.out.println(); } public static void randomfill(double[] arr) { (int = 0; < arr.length; i++) { arr[i] = (math.random() * size); } } @suppresswarnings("empty-statement") public static boolean typeofsearch(double sv, int lb, int ub) { scanner input = new scanner(system.in); int sp = 0; int success = input.nextint(); int fail = input.nextint(); int quit = input.nextint(); boolean cont = true; { system.out.println("enter type of search: "); if (success == 1) { sv = arr[sp]; interpolationsearch(arr, size, sv, lb, ub); system.out.println("true"); } else { system.out.println("enter type of search: "); if (fail == 0) { sv = arr[sp] + arr[sp + 1] / 2; interpolationsearch(arr, size, sv, lb, ub); system.out.println("false"); } } } while (cont); if(quit == -1){ system.out.println("quit search"); } return !cont; } private static void sort() { (int sorted = 1; sorted < arr.length; sorted++) { if ((arr[sorted] < arr[sorted - 1])) { (int = sorted; > 0; i--) { if (arr[i] < arr[i - 1]) { swap(i, - 1); } else { break; } } } } } private static void swap(int i, int j) { double temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
i didn't why using success, fail , quit variables. think should read type of search inside loop , use fixed code quit. example:
int typeofsearch; while ((typeofsearch=input.nextint())!=-1) { // code choose type of search based on user input }
however, couldn't understand search type options of program.
Comments
Post a Comment