Advertisement
Stelios_Gakis

Exercise_5/Task_8

Oct 6th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Task_8 {
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.  
  8.         Random rand = new Random();
  9.         int randomPlace1;
  10.         int randomPlace2;
  11.         int randomPlace3;
  12.         int randomPlace4;
  13.         int randomPlace5;
  14.         int correctRuns = 0;
  15.         System.out.println("Enter the amount of total runs");
  16.         int totalRuns = in.nextInt();
  17.  
  18.         int[] proper = {1, 2, 3, 4, 5};
  19.         int[] game = {0, 0, 0, 0, 0};
  20.         int a = 1;
  21.         int b = 2;
  22.         int c = 3;
  23.         int d = 4;
  24.         int e = 5;
  25.  
  26.         for (int ii = 0; ii < totalRuns; ii++) {
  27.             randomPlace1 = rand.nextInt(5);
  28.             game[randomPlace1] = a;
  29.  
  30.             while (true) {
  31.                 randomPlace2 = rand.nextInt(5);
  32.                 if (randomPlace2 != randomPlace1) {
  33.                     game[randomPlace1] = b;
  34.                     break;
  35.                 }
  36.             }
  37.  
  38.             while (true) {
  39.                 randomPlace3 = rand.nextInt(5);
  40.                 if (randomPlace3 != randomPlace1 && randomPlace3 != randomPlace2) {
  41.                     game[randomPlace3] = c;
  42.                     break;
  43.                 }
  44.             }
  45.  
  46.             while (true) {
  47.                 randomPlace4 = rand.nextInt(5);
  48.                 if (randomPlace4 != randomPlace1 && randomPlace4 != randomPlace2 && randomPlace4 != randomPlace3) {
  49.                     game[randomPlace4] = d;
  50.                     break;
  51.                 }
  52.             }
  53.             while (true) {
  54.                 randomPlace5 = rand.nextInt(5);
  55.                 if (randomPlace5 != randomPlace1 && randomPlace5 != randomPlace2 && randomPlace5 != randomPlace3 && randomPlace5 != randomPlace4) {
  56.                     game[randomPlace5] = e;
  57.                     break;
  58.                 }
  59.             }
  60.  
  61.             if (proper[0] == game[0] || proper[1] == game[1] || proper[2] == game[2] || proper[3] == game[3] || proper[4] == game[4]) {
  62.                 correctRuns++;
  63.             }
  64.         }
  65.         double totalRunsDouble = totalRuns;
  66.         double correctRunsDouble = correctRuns;
  67.         double correctRunsPercent = correctRunsDouble / totalRunsDouble * 100;
  68.         System.out.printf("Correct runs : %d (%.5f)%nFailed runs : %d (%.5f)%n", correctRuns, correctRunsPercent, totalRuns-correctRuns, 100-correctRunsPercent);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement