公共静态诠释binarysearch()!我不知道它的正确形式. [英] public static int binarysearch() ! i dont know the correct form of it.

查看:112
本文介绍了公共静态诠释binarysearch()!我不知道它的正确形式.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package binarysearch;
import java.util.Scanner;
public class Main {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       int []a = new int [8];
       int low=0;
       int high=7;
       
       for(int i=0 ; i<a.length ; i++)
       {
     System.out.printf("\r\nPlease enter a number :",a[i]);
     a[i] = input.nextInt();
       }

     System.out.printf("\r\nPlease enter an integer :");
        int key = input.nextInt();

  //   System.out.printf("\r\nIt is in place "(binarysearch(numbers , low  ,high ,key)");

     public Static int binarysearch(int[]numbers,int key,int low , int high){

        if(low>high)
        {
            return -1;
        }
        int mid = (low + high)/2 ;
        if(numbers [mid] == key){
            return mid;

                if(key < numbers[mid])
                {
                    return binarysearch(numbers , low , mid-1 , key);
                }     else{
                        return binarysearch(numbers , mid+1,high , key);
   System.out.printf("\r\nIt is in place "(binarysearch(numbers , low  ,high ,key)");
                }
            
        }
        }
    }



我将这段代码放在< pre></pre>之间.标签,以使其更具可读性和b)显示基本错误;

解决方案

作为练习,我将代码放在< pre></pre>之间.标签(请参见编辑窗口顶部的代码块"按钮),并更正了对齐方式,但没有更正代码.如您所见,有许多错误指示(如javac会告诉您)此代码甚至无法编译.尝试更正语法,看看会发生什么.


代码中的那个笑脸肯定不会编译...


看着空手道小子,你应该记得打蜡-打蜡".每次涂蜡时,都必须除去蜡.代码非常相似,伟大的叙利亚4号忍者代码总是说大步向前,大步向前".

最后,正如伟大的绝地武士所说:这句咒语将自己应用到你必须的身上."

package binarysearch;
import java.util.Scanner;
public class Main {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       int []a = new int [8];
       int low=0;
       int high=7;
       
       for(int i=0 ; i<a.length ; i++)
       {
     System.out.printf("\r\nPlease enter a number :",a[i]);
     a[i] = input.nextInt();
       }

     System.out.printf("\r\nPlease enter an integer :");
        int key = input.nextInt();

  //   System.out.printf("\r\nIt is in place "(binarysearch(numbers , low  ,high ,key)");

     public Static int binarysearch(int[]numbers,int key,int low , int high){

        if(low>high)
        {
            return -1;
        }
        int mid = (low + high)/2 ;
        if(numbers [mid] == key){
            return mid;

                if(key < numbers[mid])
                {
                    return binarysearch(numbers , low , mid-1 , key);
                }     else{
                        return binarysearch(numbers , mid+1,high , key);
   System.out.printf("\r\nIt is in place "(binarysearch(numbers , low  ,high ,key)");
                }
            
        }
        }
    }



I put this code between <pre></pre> tags in order to make it a) more readable and b) to show the basic mistakes; so why did you remove the tags and make it unreadable?

解决方案

As an exercise I put your code between <pre></pre> tags (see the "code block" button atop the edit window), and corrected the alignment but not the code. As you can see there are a number of errors indicating (as javac would tell you) that this code does not even compile. Try correcting the syntax and see what happens.


That smiley you have in the code certainly won''t compile...


If you ever watched Karate Kid, you shoudl remember ''wax on - wax off''. Everytime you apply the wax, you must remove the wax. Code is very similar, the great Code Ninjas of Syrias 4 always say ''Brace in, brace out''.

Finally as the great jedi would say ''This mantra apply yourself to you must.''


这篇关于公共静态诠释binarysearch()!我不知道它的正确形式.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆