Java 程序不返回

Java 程序不返回,java,recursion,divide-and-conquer,Java,Recursion,Divide And Conquer,我想在数组中找到一个“峰值”(a1ai+1>..>an的值,这里ai是峰值)。我在这里使用“分而治之”来获得更优的解决方案。对于“6 1 3 50 70 100 48”,它将打印“70 100 48 4”,这是好的(7048),但它不返回整数。对于字符串(a[m]),它返回“数组没有峰值”。我尝试删除字符串并使用int,但我遇到了完全相同的问题 public class Main { public int n, a[]; void read() { File

我想在数组中找到一个“峰值”(a1ai+1>..>an的值,这里ai是峰值)。我在这里使用“分而治之”来获得更优的解决方案。对于“6 1 3 50 70 100 48”,它将打印“70 100 48 4”,这是好的(70<100和100>48),但它不返回整数。对于字符串(a[m]),它返回“数组没有峰值”。我尝试删除字符串并使用int,但我遇到了完全相同的问题

public class Main {

    public int n, a[];

    void read() {
        File file = new File("src/com/fmi/Vector.txt");
        Scanner sc;

        try {
            sc = new Scanner(file);
            int i = 0;
            if (sc.hasNextInt()) {
                n = sc.nextInt();
            }
            a = new int[n];
            while (sc.hasNextInt()) {
                int aux = sc.nextInt();
                a[i] = aux;
                i++;
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
        }
    }

    String search(int p, int u) {
        int m;
        System.out.println(p + " " + u);
        if (p == u) {
            return "0";
        } else {
            m = (p + u) / 2;
            System.out.println(a[m - 1] + " " + a[m] + " " + a[m + 1] + " " + m);
            if (a[m - 1] < a[m] && a[m] > a[m + 1]) {
                return Integer.toString(a[m]);
            } else if (a[m - 1] < a[m] && a[m] < a[m + 1]) {
                search(m, u);
            } else if (a[m - 1] > a[m] && a[m] > a[m + 1]) {
                search(p, m);
            }
        }
        return "Array has no peak!";
    }

    void display() {
        for (int i = 0; i < n; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
        for (int i = 0; i < n; i++) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        Main obj = new Main();
        obj.read();
        System.out.println(obj.search(0, obj.n));
        obj.display();
    }
}
公共类主{
公共int n,a[];
无效读取(){
File File=新文件(“src/com/fmi/Vector.txt”);
扫描仪sc;
试一试{
sc=新扫描仪(文件);
int i=0;
if(sc.hasnetint()){
n=sc.nextInt();
}
a=新整数[n];
while(sc.hasnetint()){
int aux=sc.nextInt();
a[i]=aux;
i++;
}
}catch(filenotfounde异常){
System.out.println(“未找到文件!”);
}
}
字符串搜索(int p,int u){
int m;
系统输出打印项次(p+“”+u);
如果(p==u){
返回“0”;
}否则{
m=(p+u)/2;
System.out.println(a[m-1]+“”+a[m]+“”+a[m+1]+“”+m);
如果(a[m-1]a[m+1]){
返回整数.toString(a[m]);
}否则如果(a[m-1]a[m]&a[m]>a[m+1]){
搜索(p,m);
}
}
return“数组没有峰值!”;
}
无效显示(){
对于(int i=0;i
您正在调用搜索,但没有对结果执行任何操作。 你应该这样做

return search(m,u);

请格式化您的代码。很难读取。请删除文件读取并直接在代码中添加值。这既确保了问题不在文件读取上,又允许我们重现错误,并尝试简化这些条件。您是否调试并检查了实际的搜索调用和条件测试值?您的意思是
返回搜索(m,u)
返回搜索(p,m)
而不是
搜索(m,u)
搜索(p,m)
?哦,我错过了。谢谢