fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String player1 = "1465"; // solution
  9. String player2 = "6431"; // guess
  10.  
  11. Integer cows = 0, bulls = 0, k;
  12.  
  13. for (int i = 0, j = player2.length(); i < j; i++)
  14. {
  15. Character ch = player2.charAt(i);
  16. String s = ch.toString();
  17.  
  18. if (player1.length() > i && player1.charAt(i) == ch) {
  19. bulls++;
  20. }
  21. else if (0 <= (k = player1.indexOf(s))) {
  22. cows++;
  23. }
  24. }
  25.  
  26. System.out.println("BULLS: " + Integer.toString(bulls));
  27. System.out.println("COWS: " + Integer.toString(cows));
  28. }
  29. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
BULLS: 1
COWS:  2