+ 1
What is wrong with my code
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the len:"); int len = sacn.nextInt(); ArrayList<Integer>ar = new ArrayList<Integer>; int sum = 0; int arr[] = new int[len]; for(int i =0;i<lrn;i++) { a[i] = scan.nextInt(); } for(int k = 0;k<len;k++) { for(int o;o<(k+1);o++) { for(int u=k;u<=o;k++) { sum = sum+arr[u]; } ar.add(sum); } } } }
1 Respuesta
+ 1
//Typo mistakes : in sacn, a[] but declared arr[] array
//Ravi King corrected code , removed errors.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the len:");
int len = scan.nextInt(); //you are using sacn.n..
ArrayList<Integer> ar = new ArrayList<Integer>(); //missing ()
int sum = 0;
int arr[] = new int[len];
for(int i =0;i<len;i++) {
arr[i] = scan.nextInt(); //you are using a[I] which is not declared any where
}
for(int k = 0;k<len;k++) {
for(int o=0;o<(k+1);o++) { //'o' is uninitialized, use int o=0; or what you need!
for(int u=k;u<=o;u++) { //incrementing k++ , instead of u++ so it makes infinite loop.
sum = sum+arr[u];
}
ar.add(sum);
}
}
System .out.print(ar); //for outut some : you are not printing anything so it won't output anything ..
}
}