gpt4 book ai didi

java - 我的代码中 return 语句错误

转载 作者:行者123 更新时间:2023-12-01 06:47:01 26 4
gpt4 key购买 nike

我在第 16 行收到 return 语句错误。

如何解决这个问题?

import java.util.Scanner;
class abc
{
String str;
Scanner sc=new Scanner(System.in);
int mid(int l,int h)
{
System.out.println((l+h)/2);
str=sc.nextLine();
if(str.indexOf('L')==0)
mid(((l+h)/2+1),h);
else if(str.indexOf('H')==0)
mid(l,((l+h)/2)-1);
else if(str.indexOf('w')==0)
return 1;
}


}
public class Main {
public static void main(String[] args){
int a,b;
abc o;
Scanner in=new Scanner(System.in);
a=in.nextInt();
b=in.nextInt();
o.mid(a,b);
}
}

最佳答案

您需要在所有控制路径上返回:

int mid(int l,int h)
{
System.out.println((l+h)/2);
str=sc.nextLine();
if(str.indexOf('L')==0)
return mid(((l+h)/2+1),h); //add return statement here
else if(str.indexOf('H')==0)
return mid(l,((l+h)/2)-1); //and here
else if(str.indexOf('w')==0)
return 1;
}

关于java - 我的代码中 return 语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8590435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com