-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomNumber.java
More file actions
36 lines (29 loc) · 915 Bytes
/
RandomNumber.java
File metadata and controls
36 lines (29 loc) · 915 Bytes
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
package homework;
import java.util.Random;
public class RandomNumber {
// return highest digit of random three-digit number
public static int randomNumber(int lowerN, int upperN) {
if (lowerN > upperN) {
int tmp = lowerN;
lowerN = upperN;
upperN = tmp;
}
return (new Random()).nextInt((upperN - lowerN) + 1) + lowerN;
}
public static char maxOfTwo(char a, char b) {
return a > b ? a : b;
}
public static char maxDigit(int num) {
char[] numArr = ("" + num).toCharArray();
char max = numArr[0];
for (int i = 1; i < numArr.length; i++) {
max = maxOfTwo(max, numArr[i]);
}
return max;
}
public static void main(String... args) {
int randN = randomNumber(100, 999);
System.out.println(randN);
System.out.println(maxDigit(randN));
}
}