5
Could someone tell me what’s wrong with this code? The result is always the first number before the space, but has to be the sum of all numbers in the array.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner entrada = new Scanner(System.in);
String x = entrada.next();
String array[] = x.split (" ");
int soma = 0;
int resultado[] = new int[array.length];
for (int i = 0; i < array.length; i++) {
resultado[i] = Integer.parseInt(array[i]);
soma = soma + resultado[i];
}
System.out.println (soma);
}
}
Always 1? No! I just took a test and gave 2. Always give the first number of the sequence.
– user6406