Skip to content

Instantly share code, notes, and snippets.

@afutseng
Created December 18, 2011 09:42
Show Gist options
  • Save afutseng/1492876 to your computer and use it in GitHub Desktop.
Save afutseng/1492876 to your computer and use it in GitHub Desktop.
Java dynamic array input test
import java.util.*;
public class DynamicInputTest {
public static void main(String[] argv) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] numbers = new int[n]; // numbers is n size' s array
// Input & summation
int sum = 0;
for (int i = 0; i < n; i++) {
numbers[i] = sc.nextInt();
sum += numbers[i];
}
// Output such as 1+2+3+...+n = [sum]
System.out.print(numbers[0]);
for (int i = 1; i < n; i++) {
System.out.print("+" + numbers[i]);
}
System.out.println(" = " + sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment