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.
When evenArray & oddArray have values after the last for-loop finished. You just use 2 another loop to print this 2 array.
7 is evenCounter + 1
5 is oddCounter + 1
Ex: String s = "These " + (evenCounter + 1) + "numbers are even: " .... print your array.
Just for your output:
System.out.print("These "+evenCounter+" numbers are even: ");
for (int i = 0; i < evenCounter; i++){
System.out.print(evenArray[i]+" ");
}
System.out.println();
System.out.print("These "+oddCounter+" numbers are odd: ");
for (int i = 0; i < oddCounter; i++){
System.out.print(oddArray[i]+" ");
}
System.out.println();
Could work, there are other solutions. For example:
String evStr=new String();
String odStr=new String();
int curNum=0;
for (int i = 0; i < antal; i++) {
curNum = (int) (Math.random() * 999 + 1);
System.out.print(curNum + " ");
if ((curNum % 2) == 0) {
evStr+=" "+curNum;
evenCounter++;
}
else {
odStr+=" "+curNum;
oddCounter++;
}
}
System.out.println();
System.out.println("These "+evenCounter+" numbers are even: "+evStr);
System.out.println("These "+oddCounter+" numbers are odd: "+odStr);
So the whole second part with even/odd arrays of your code disappears. And there are no arrays at all any more.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.