Advertisement
Guest User

W5 - Ex. 11

a guest
Oct 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OneDimensionalMemory {
  4.  
  5.     private Scanner cin=new Scanner(System.in);
  6.  
  7.     private int[] a={1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
  8.  
  9.     private int[] b=new int[10];
  10.  
  11.     private boolean ok=false;
  12.  
  13.     private void shuffle(int[] a) {
  14.  
  15.         int temp;
  16.  
  17.         for (int i=0; i<100; i++) {
  18.  
  19.             int r1=(int) (Math.random()*a.length);
  20.  
  21.             int r2=(int) (Math.random()*a.length);
  22.  
  23.             temp=a[r1];
  24.  
  25.             a[r1]=a[r2];
  26.  
  27.             a[r2]=temp;
  28.  
  29.         }
  30.  
  31.     }
  32.  
  33.     private int input() {
  34.  
  35.         int num;
  36.  
  37.         while (true) {
  38.  
  39.             try {
  40.  
  41.                 num=Integer.parseInt(cin.nextLine())-1;
  42.  
  43.                 if (num>=0 && num<10) {
  44.  
  45.                     break;
  46.  
  47.                 } else {
  48.  
  49.                     System.out.print("The number has to be from 1 to 10. Try again: ");
  50.  
  51.                 }
  52.  
  53.             } catch (Exception e) {
  54.  
  55.                 System.out.print("The value you entered is not valid. Try again: ");
  56.  
  57.             }
  58.  
  59.         }
  60.  
  61.         return num;
  62.  
  63.     }
  64.  
  65.     private void printUserChoice(int[] a, int x, int y) {
  66.  
  67.         int k=0;
  68.  
  69.         System.out.print("[ ");
  70.  
  71.         for (int i=0; i<a.length; i++) {
  72.  
  73.             if ((i==x || i==y)) {
  74.  
  75.                 System.out.print(a[i] + " ");
  76.  
  77.             } else if (a[i]==b[i]) {
  78.  
  79.                 System.out.print(a[i] + " ");
  80.  
  81.             } else {
  82.  
  83.                 System.out.print(". ");
  84.  
  85.             }
  86.  
  87.         }
  88.  
  89.         System.out.println("]");
  90.  
  91.         if (a[x]==a[y]) {
  92.  
  93.             System.out.println("Lucky!");
  94.  
  95.             k++;
  96.  
  97.             b[x]=a[x];
  98.  
  99.             b[y]=a[y];
  100.  
  101.         } else {
  102.  
  103.             System.out.println("Too bad, try again!");
  104.  
  105.         }
  106.  
  107.         if (k==5) {
  108.  
  109.             ok=true;
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.     private void run() {
  116.  
  117.         int x, y;
  118.  
  119.         shuffle(a);
  120.  
  121.         while (!ok) {
  122.  
  123.             System.out.print("Enter index one (1-10): "); x=input();
  124.  
  125.             System.out.print("Enter index two (1-10): "); y=input();
  126.  
  127.             while (x==y) {
  128.  
  129.                 System.out.print("The second index can't be the same with the first one. Try again: ");
  130.  
  131.                 y=input();
  132.  
  133.             }
  134.  
  135.             printUserChoice(a, x, y);
  136.  
  137.         }
  138.  
  139.         System.out.println("You found all the pairs. Nice!");
  140.  
  141.     }
  142.  
  143.     public static void main(String[] args) {
  144.  
  145.         new OneDimensionalMemory().run();
  146.  
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement