如何获取用户输入以使用正确的说明并退出程序?

时间:2015-07-14 01:37:12

标签: java interpolation

所以我对布尔方法有问题' typeOfSearch'要求用户输入要使用的搜索类型,无论是成功,失败还是退出搜索。我使用的方法不承认输入成功或失败,但它确实用于退出输入,导致它递归。我究竟做错了什么?谢谢你的建议!

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) {
        for (double num : arr) {
            System.out.println(num + "");
        }
        System.out.println();
    }

    public static void randomFill(double[] arr) {
        for (int i = 0; i < 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;
        do {
            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() {
        for (int sorted = 1; sorted < arr.length; sorted++) {
            if ((arr[sorted] < arr[sorted - 1])) {
                for (int i = sorted; i > 0; i--) {
                    if (arr[i] < arr[i - 1]) {
                        swap(i, i - 1);
                    } else {
                        break;
                    }
                }
            }
        }
    }

    private static void swap(int i, int j) {
        double temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}

1 个答案:

答案 0 :(得分:0)

我不明白为什么你使用成功,失败和退出变量。我认为您应该只读取循环内的搜索类型并使用固定代码退出。例如:

int typeOfSearch;

while ((typeOfSearch=input.nextInt())!=-1) {
// code to choose the type of search based on user input
}

但是,我无法理解您的程序的搜索类型选项。