Skip to content

Instantly share code, notes, and snippets.

@ilhamarrouf
Created December 9, 2018 12:51
Show Gist options
  • Save ilhamarrouf/e11d28698b62df53f5f8f83a9c3ca963 to your computer and use it in GitHub Desktop.
Save ilhamarrouf/e11d28698b62df53f5f8f83a9c3ca963 to your computer and use it in GitHub Desktop.
Intorduction to Programming
import java.util.Random;
import java.util.Scanner;
public class RandomExample {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int random;
System.out.print("Masukkan bilangan a: ");
int a = Integer.parseInt(reader.nextLine());
System.out.print("Masukkan bilangan b: ");
int b = Integer.parseInt(reader.nextLine());
Random rand = new Random();
if(a >= b) {
random = rand.nextInt(a - b + 1) + b;
} else {
random = rand.nextInt(b - a + 1) + a;
}
if((random % 2)==0) {
System.out.print("Hasil Random bilangan genap: " + random);
} else {
System.out.print("Hasil Random bilangan genap: " + (random + 1));
reader.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment