【问题标题】:Error is shown. I wanna return the value from If(){}显示错误。我想从 If(){} 返回值
【发布时间】:2017-10-31 19:22:27
【问题描述】:

这是我的二分搜索程序。我正在寻找Item=a[4]=5。如何从 while 循环中存在的 if 语句返回值,而不在循环外声明 return 语句。

package Array;

class myArray
{
    int a[]= {1,2,3,4,5,6,7,8,9,10};
    //this block is only for printing the array
    {
        for(int i=0; i<=9;i++)
        {
            System.out.println(a[i]);
        }
    }
    public int LocationFinder()
    {   
        int Start=a[0],End=a[9],Item=a[4],loc=0;
        while(Start<=End)
        {
            int mid=(Start+End)/2;
            if(Item==mid)
            {
            Item=loc;
            return loc; // <-- I want to return from here
            }
            else if(Item>mid)
            {
                Start=mid+1;
            }
            else
                End=mid-1;


        }

    }

我想从上述位置返回,但发生错误。我怎样才能摆脱这个错误?

【问题讨论】:

  • 您遇到什么错误?包括日志或堆栈跟踪。
  • 首先你缺少return's,这甚至不会编译。
  • 如果Start &gt; End 会发生什么? while 循环结束,方法返回..... what?!?
  • 这是二分查找程序我如何从 if 语句中返回值???
  • 标题说“显示错误”,但您实际上并没有向我们展示您得到的错误,类似于“此方法必须返回 int 类型的结果",因为循环后没有return语句,循环可以结束,那么返回值是多少呢?

标签: java


【解决方案1】:

你的方法应该是这样的:

使用索引:

public int LocationFinder()
    {   
          int Start=0,End=arr.length - 1,Item=a[4];
//Item = key 

while(Start<=End)
        {
            int mid=Start + (End-Start)/2;
            //Checking if item is present at mid     
            if(arr[mid]==Item)
            {
            return mid; //returning the index 
            }
            //if item is greater, than ignore the left side
            else if(arr[mid]<Item)
            {
                Start=mid+1;
            }
            //if item is smaller, than ignore the right side
            else
                End=mid-1;
 }
//returning -1 if the value is not found
return -1;
}

使用值:

   public int LocationFinder()
            {   
          int Start=arr[0],End=arr[length - 1],Item=a[4];
//Item = key 

        while(Start<=End)
                {
                    int mid=(Start + End)/2;
                    //Checking if item is present at mid     
                    if(arr[mid]==Item)
                    {
                    return mid; //returning the index 
                    }
                    //if item is greater, than ignore the left side
                    else if(arr[mid]<Item)
                    {
                        Start=mid+1;
                    }
                    //if item is smaller, than ignore the right side
                    else
                        End=mid-1;
         }
        //returning -1 if the value is not found
        return -1;
        }

为了将来的帮助,请参阅:

Binary Search

【讨论】:

  • 基本上,在第一个if语句中,写“return mid”
  • 谢谢你对我的帮助更多谢谢
  • @ShahirZain 我总是乐于帮助人们,当您遇到问题时提出更多问题。
  • 是的,我还有一个问题。 . .如何使用没有访问修饰符的接口方法???
  • @ShahirZain 看到这个:tutorialspoint.com/java/java_interfaces.htm 并从 tuotrialpoint 学习 OOP 和 Java 的基础知识,当您在编码中遇到问题时,将其与代码一起发布到 stackoverflow 上,以便您获得帮助。碰巧编码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
相关资源
最近更新 更多