
class Arrayd
{
static int max(int x[]){
int i,max;
max=x[0];
for(i=0;i<x.length;i++)
{
if(x[i]>max)
{
x[i]=max;
}
}
return(max);
}
}
public static void main(String... s)
{
int k=0;
System.out.println("\n...Calling a function to print the maximum no. occuring in an array...");
k=Arrayd.max(new int[]{10,20,30,40,50});
System.out.println("Maximum value=");
System .out.println(k);
}
Everytime i compile this program in the terminal it gives me:
class,interface or enum expected.
Please help me with this error!

You have written main method outside the class. Put the main method inside the class. Also, your code displays 10 as the maximum number which is wrong, output should be 50.So we have modified your code.Check it:
class Arrayd
{
static int max(int x[]){
int largest=x[0];
for(int i=1;i < x.length;i++){
if(x[i] > largest){
largest = x[i];
}
}
return largest;
}
public static void main(String... s)
{
int k=0;
System.out.println("\n...Calling a function to print the maximum no. occuring in an array...");
k=Arrayd.max(new int[]{10,20,30,40,50});
System.out.println("Maximum value=");
System .out.println(k);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.