I'm stuck with my program. What it does, is basically it generates 100 random numbers into an array. It prints the numbers in 4 lines. After that, another for loop checks if there are numbers, which can be divided by seven, and are even at the same time. If there are, it inserts them into another array. Now, we don't know how many numbers like that are in the random array, so that's where I stuck basically. I have the sum of the numbers in the array. But can't calculate the pieces of elements.
import static java.lang.System.out;
import java.util.Random;
public class CSharp {
public static void main(String[] args) {
Random r = new Random();
int[] numbers = new int[100];
int[] canBeDividedBySeven = new int[100];
int c = 0;
int sum = 0;
int average = 0;
out.println("Random numbers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] += r.nextInt(90) + 10;
out.print(numbers[i] + " ");
if (i % 35 == 0) {
out.print("\n");
}
}
out.println("\n\nNumbers that can be divided by 7 and are even: ");
for (int j = 0; j < numbers.length; j++) {
if (numbers[j] % 7 == 0 && numbers[j] % 2 == 0) {
out.print(numbers[j] + " ");
canBeDividedBySeven[c] += numbers[j];
}
}
out.println("\n\nThe sum of the numbers in the 'canBeDividedBySeven': ");
for (int y = 0; y < canBeDividedBySeven.length; y++) {
sum += canBeDividedBySeven[y++];
}
out.print(sum);
}
}
Aucun commentaire:
Enregistrer un commentaire