Program has error lines which are remarked by ???? .I could not understand where the problem is
public static void main (String args[]) {
Scanner myinput=new Scanner(System.in) ; //Arrary length comes from user!
System.out.println("Enter a number: ") ;
int sayi=myinput.nextInt() ;
int [] Array = new int [sayi] ;
for(int i=0; i<SayiDizisi.length ; i++){ //Fill 开发者_JAVA技巧the array!(Comes from user)
System.out.println("Enter the numbers: ") ;
SayiDizisi[i]=myinput.nextInt() ;}
}
Max(int [] SayiDizisi) ; // ???????????????????????????????????
}
public static int Max(int [] Array1) {
int max=SayiDizisi1[0] ;
for(int i=0; i<SayiDizisi1.length ; i++) {
if(SayiDizisi1[i]>max)
max=SayiDizisi1[i] ;
}
return SayiDizisi ; //?????????????????????
}
}
For a start, you should not have the int []
type in the method call. Change:
Max (int[] SayiDizisi);
to:
Max (SayiDizisi);
Secondly, your Max
function is expected to return an integer but it's returning an array of integers. Change its return statement from:
return SayiDizisi;
to:
return max;
Thirdly, I can't see where SayiDizisi
is defined anywhere. You appear to be creating an array to be populated but you've called it Array
. Change:
int[] Array = new int[sayi];
to:
int[] SayiDizisi = new int[sayi];
Finally, your braces are not balanced. See the line SayiDizisi[i]=myinput.nextInt() ;}
- it has a superfluous brace at the end which causes the compiler to misunderstand your intent.
Here's a fully functioning one with those fixes made, and some comments added for your education:
import java.util.Scanner;
public class scratch {
public static void main (String args[]) {
// Get the count and allocate array.
Scanner myinput = new Scanner (System.in);
System.out.println ("Enter a number: ");
int sayi = myinput.nextInt();
int[] SayiDizisi = new int[sayi];
// Get the values into the array.
for (int i = 0; i < SayiDizisi.length ; i++) {
System.out.println ("Enter the numbers: ");
SayiDizisi[i] = myinput.nextInt();
}
// Output the maximum.
int x = Max (SayiDizisi);
System.out.println ("Maximum is: " + x);
}
public static int Max(int[] Array1) {
// Assume first is largest.
int max = Array1[0] ;
// Check all others, finding larger.
for (int i = 0; i < Array1.length ; i++) {
if (Array1[i] > max)
max = Array1[i];
}
// Return the largest found.
return max;
}
}
精彩评论