Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shavit Borisov
- // CW
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- ExactHit obj = new ExactHit();
- int current;
- System.out.printf("Enter a 4-digits number and hope to succeed! ");
- current = in.nextInt();
- while(!(obj.guess(current).equals("finished")))
- {
- System.out.printf("%s\n", obj.guess(current));
- System.out.printf("Keep trying! ");
- current = in.nextInt();
- }
- System.out.printf("%s", obj.guess(current));
- in.close();
- }
- }
- // Next class
- import java.util.Random;
- public class ExactHit
- {
- private int[] numbers = new int[4];
- public ExactHit()
- {
- Random r = new Random();
- for(int i = 0; i < numbers.length; i++)
- numbers[i] = r.nextInt(10);
- }
- public String guess(int input)
- {
- int exacts = 0;
- int hits = 0;
- int currentNum;
- for(int i = 0; i < numbers.length; i++)
- {
- currentNum = input % 10;
- if(currentNum == numbers[i])
- {
- exacts++;
- }
- else
- {
- for(int j = 0; j < numbers.length; j++)
- {
- if(j == i)
- continue;
- if(currentNum == numbers[j])
- {
- hits++;
- break;
- }
- }
- }
- input /= 10;
- }
- if(exacts == 4)
- return "finished";
- else
- return exacts + " exacts and " + hits + " hits";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement