0
What is the problem in my code?
import java.util.*; class program { public static void main(String args[]) { int num; int c=1; int gap; int n; int[] arr = new int[n]; Scanner sc = new Scanner(System.in); System.out.println("Enter array size"); n = sc.nextInt(); for(i = 0; i < arr.length;i++) { System.out.println("Enter" + c + "number"); num=sc.nextInt(); arr[i] = num; c++; } gap = arr[arr.length-1] - arr[0]; System.out.println("The longest gap is" + gap); } }
1 Answer
+ 1
I ran the code the first error rose at the array,Create the scanner object at the start of the program then get 'n' from the user(error solved)..The next error happened at the first for loop,Add the type 'int' to i in the loop,"i" alone wont work without a type...