In: Computer Science
For some reason another programmer has asked you to write a function.
The function is called getWarYear ( ) It has one parameter , an integer.
if it is 1 return a random number in the range of 1914 to 1919.
if it is 2 return a random number in the range of 1938 to 1945.
Since the function is returning an year the data type of the return value is an integer.
import java.util.Random;
public class RandomYear {
public static void main(String[] args) {
System.out.println(getWarYear(1));
System.out.println(getWarYear(2));
}
private static int getWarYear(int n) {
int min = 0, max = 0;
// setting min and max values based
on given string
if (n == 1) {
min =
1914;
max =
1919;
}
if (n == 2) {
min =
1938;
max =
1945;
}
// generating random number in
range
Random r = new Random();
return r.nextInt((max - min) + 1) +
min;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me