//Java class Main { public static void main(String[] args){ System.out.print("Input n: "); int n = new java.util.Scanner(System.in).nextInt(); int[] a = new int[n];
int b = 0; int c = 0; int d;
for (int i = 0; i < a.length; i++) { d = new java.util.Random().nextInt(2); a[i] = new java.util.Random().nextInt(100000); if (d == 0) a[i] = -a[i]; if (a[i] > 0) b+=a[i]; else c+=a[i]; } System.out.println("Positive: " + b); System.out.println("Negative: " + c); } }
class Main {
public static void main(String[] args){
System.out.print("Input n: ");
int n = new java.util.Scanner(System.in).nextInt();
int[] a = new int[n];
int b = 0;
int c = 0;
int d;
for (int i = 0; i < a.length; i++) {
d = new java.util.Random().nextInt(2);
a[i] = new java.util.Random().nextInt(100000);
if (d == 0)
a[i] = -a[i];
if (a[i] > 0)
b+=a[i];
else
c+=a[i];
}
System.out.println("Positive: " + b);
System.out.println("Negative: " + c);
}
}
Проверка:
$ javac Main.java
Input n: 15
Positive: 472449
Negative: -195366