Frage

Ok so I have to make a java program to search through an array using a binary search to find 45.3 but I am getting numerous class, interface, or enum expected errors starting after public static void main(String[] args) on line 23 can you guys help. thanks.

public class BinSearch
{

public static final int NOT_FOUND = -1;

public static int binarySearch(double[] arr, double x)
{
    int low = 0;
    int high = arr.length - 1;
    int mid;
    while (low <= high)
    {
                mid = (low + high) / 2;

                if (arr[mid] > x)
        {
                  high = mid - 1;

                }
        else if (arr[mid] < x)
        {

          low = mid + 1;
                }
        else
        {
                   return mid+1;
                }
        }
    return NOT_FOUND;
}


public static void main(String[] args)
{
int j,x;
    double y,temp;
double[] arg= {-3.0, 10.0, 5.0, 24.0, 45.3, 10.5};

int i=0;
    for (j = 1; j<arg.length;j++)
{
if(arg[i]>arg[j])
{
    temp = arg[i];
    arg[i] = arg[j];
    arg[j] = temp;
}
i++;
System.out.print(arg[j-1]+",");
}


x = binarySearch(arg, 45.3);
System.out.print("45.3 found at ");
System.out.print(x);
 }
}
War es hilfreich?

Lösung

It looks like you tried to use public class BinSearch as your main class. However, your parentheses enclose code before your main args.

     public class MyClass {

    //}// no bracket here
            public static void main(String[] args) {

    }
}

If you want to use a class to create Objects that use methods such as your public static final int , create a new class before.

class NewClass {

}

public class MyClass {


            public static void main(String[] args) {

    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top