My program should look like this:
How many random numbers between 0 - 999 do you want? 12 (user input)
Here are the random numbers: 145 538 56 241 954 194 681 42 876 323 2 87
These 7 numbers are even: 538 56 954 194 42 876 2
These 5 numbers are odd: 145 241 681 323 87
I cannot seem too place the String
s ("These 7 numbers are even"
and "These 5 numbers are odd"
at the right spot in the code. And how do I print the exact number (7
and 5
) of even and odd numbers that are displayed in the String
? The code does not seem to work either way for me. Do you see the problem(s)? Well I hope you can help me figure this out, have been doing this for 12 hours straight now.
Here is my code:
import java.util.Scanner;
public class SlumpadeTal {
public static void main(String[] args) {
int evenCounter = 0;
int oddCounter = 0;
Scanner input = new Scanner(System.in);
System.out.println("How many random numbers between 0 - 999 do you want?");
int antal = input.nextInt();
System.out.println("Here are the random numbers:");
int[] arrayen = new int[antal];
for (int i = 0; i < arrayen.length; i++) {
arrayen[i] = (int) (Math.random() * 999 + 1);
System.out.print(arrayen[i] + " ");
if ((arrayen[i] % 2) == 0) {
evenCounter++;
}
else {
oddCounter++;
}
}
int[] evenArray = new int [evenCounter];
int[] oddArray = new int[oddCounter];
evenCounter = 0;
oddCounter = 0;
for (int i = 0; i < arrayen.length; i++){
if ((arrayen[i] % 2) == 0) {
evenArray[evenCounter] = arrayen[i];
evenCounter++;
}
else {
oddArray[oddCounter] = arrayen[i];
oddCounter++;
}
}
}
}
NOTE I cannot use any class such as ArrayList
, Vector
and so on.
Aucun commentaire:
Enregistrer un commentaire