Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Task_8 {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- Random rand = new Random();
- int randomPlace1;
- int randomPlace2;
- int randomPlace3;
- int randomPlace4;
- int randomPlace5;
- int correctRuns = 0;
- System.out.println("Enter the amount of total runs");
- int totalRuns = in.nextInt();
- int[] proper = {1, 2, 3, 4, 5};
- int[] game = {0, 0, 0, 0, 0};
- int a = 1;
- int b = 2;
- int c = 3;
- int d = 4;
- int e = 5;
- for (int ii = 0; ii < totalRuns; ii++) {
- randomPlace1 = rand.nextInt(5);
- game[randomPlace1] = a;
- while (true) {
- randomPlace2 = rand.nextInt(5);
- if (randomPlace2 != randomPlace1) {
- game[randomPlace1] = b;
- break;
- }
- }
- while (true) {
- randomPlace3 = rand.nextInt(5);
- if (randomPlace3 != randomPlace1 && randomPlace3 != randomPlace2) {
- game[randomPlace3] = c;
- break;
- }
- }
- while (true) {
- randomPlace4 = rand.nextInt(5);
- if (randomPlace4 != randomPlace1 && randomPlace4 != randomPlace2 && randomPlace4 != randomPlace3) {
- game[randomPlace4] = d;
- break;
- }
- }
- while (true) {
- randomPlace5 = rand.nextInt(5);
- if (randomPlace5 != randomPlace1 && randomPlace5 != randomPlace2 && randomPlace5 != randomPlace3 && randomPlace5 != randomPlace4) {
- game[randomPlace5] = e;
- break;
- }
- }
- if (proper[0] == game[0] || proper[1] == game[1] || proper[2] == game[2] || proper[3] == game[3] || proper[4] == game[4]) {
- correctRuns++;
- }
- }
- double totalRunsDouble = totalRuns;
- double correctRunsDouble = correctRuns;
- double correctRunsPercent = correctRunsDouble / totalRunsDouble * 100;
- System.out.printf("Correct runs : %d (%.5f)%nFailed runs : %d (%.5f)%n", correctRuns, correctRunsPercent, totalRuns-correctRuns, 100-correctRunsPercent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement