Reputation: 5
I have code who makes random numbers and prints them. And it counts numbers who are bigger than 250. How can I output in a one number? I need sum the results numbers.
Like this:
315 32 486 311 58
430 145 83 395 312
223 455 370 151 84
163 415 433 75 194
152 457 427 218 301
142 298 300 404 24
15 296 368 196 438
102 410 351 341 328
results:
3 3 2 2 3 3 3 4
Random rnd = new Random();
if (ch=='Y' || ch=='y') {
for (i=0; i<8; i++)
for (j=0; j<5; j++)
A[i][j] = rnd.nextInt((500 - 10) + 1) + 10;
}
else
if (ch != 'N' && ch != 'n') {
System.out.println("input-output error");
return;
}
for (i=0; i<8; i++) {
for (j=0; j<5; j++)
System.out.print(A[i][j] + "\t");
System.out.println();
}
System.out.println("results: ");
for (i=0; i<8; i++) {
res = 0;
for (j=0; j<5; j++) {
if (A[i][j] >= 250) res++;
}
System.out.print((res) + " ");
}
}
}
Upvotes: 0
Views: 88
Reputation: 398
You can count the numbers that are greater than threshold in the loop that prints them, like this:
int res = 0;
for (i=0; i<8; i++) {
for (j=0; j<5; j++) {
System.out.print(A[i][j] + "\t");
if (A[i][j] >= 250) res++;
}
System.out.println();
}
System.out.println("results: " + res);
Upvotes: 1