-
Notifications
You must be signed in to change notification settings - Fork 0
/
randGen.java
51 lines (43 loc) · 1.39 KB
/
randGen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.science.anarky;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import static java.lang.Character.getNumericValue;
public class randGen {
private static String userInput;
private static Random rand;
public static void main(String[] args) {
rand = new Random();
ArrayList<Integer> randNum = new ArrayList<Integer>();
Scanner scn = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
System.out.print("Please enter a number: ");
userInput = scn.next();
for (int n = 0; n <= userInput.length() - 1; n++) {
if (getNumericValue(userInput.charAt(n)) % 2 == 0) {
randNum.add(oddRandNo());
} else if (getNumericValue(userInput.charAt(n)) % 2 != 0) {
randNum.add(evenRandNo());
}
}
String stringRandNum = null;
for (int m : randNum) {
stringRandNum = String.valueOf(sb.append(m));
}
System.out.println("Your random number is " + stringRandNum);
}
public static int oddRandNo() {
int x = rand.nextInt(9);
while(x%2 == 0) {
x = rand.nextInt(9);
}
return x;
}
public static int evenRandNo() {
int z = rand.nextInt(9);
while(z%2 != 0) {
z = rand.nextInt(9);
}
return z;
}
}