0
How can i edit this program to the method that will require user to enter 20 numbers instead of ask the number of element to user. And out the maximum number as this program's output?
import java.util.Scanner; public class Largest_Number { public static void main(String[] args) { int n, max; Scanner s = new Scanner(System.in); System.out.print("Enter number of elements in the array:"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter elements of array:"); for(int i = 0; i < n; i++) { a[i] = s.nextInt(); } max = a[0]; for(int i = 0; i < n; i++) { if(max < a[i]) { max = a[i]; } } System.out.println("Maximum value:"+max); } }
3 odpowiedzi
0
delete
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
and rewrite to this
int a[]=new int[20];
0
Thank you, but i got this error
Error : Variable n might not have been initialized
for(int i=0;<n;i++)
0
i am just changing your first part of code
import java.util.Scanner;
public class Largest_Number
{
public static void main(String[] args)
{
int n=20, max,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter 20 numbers to find max in them");
int a[] = new int[n];
System.out.println("Enter elements of array:");
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
max = a[0];
for(i = 0; i < n; i++)
{
if(max < a[i])
{
max = a[i];
}
}
System.out.println("Maximum value:"+max);
}
}
and in another answer, that you have write as
for(int i=0;<n;i++)
for two for loop, you have initialized two int i in one program. and you did not give any condition as well. try this for loop
for(i=0;i<n;i++)