Advertisement
Guest User

QuickLock

a guest
Oct 18th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lock {
  4. int firstNum;
  5. int secondNum;
  6. int thirdNum;
  7.  
  8. public void init(){
  9. firstNum = (int)(Math.random()*10);
  10. secondNum = (int)(Math.random()*10);
  11. thirdNum = (int)(Math.random()*10);
  12. }
  13.  
  14. public int first(){
  15. return firstNum;
  16. }
  17. public int second(){
  18. return secondNum;
  19. }
  20. public int third(){
  21. return thirdNum;
  22. }
  23.  
  24. public void open(){
  25. System.out.println("Lock Opened!");
  26. }
  27. }
  28.  
  29.  
  30. public class Main {
  31. public static void main(String []args){
  32. int numOne;
  33. int numTwo;
  34. int numThree;
  35. int tries = 3;
  36. boolean open = false;
  37.  
  38. //setup lock
  39. Lock lock = new Lock();
  40. lock.init();
  41.  
  42. Scanner input = new Scanner(System.in);
  43.  
  44. do{
  45. System.out.println("Enter three numbers between 1 and 10.");
  46. numOne = input.nextInt();
  47. numTwo = input.nextInt();
  48. numThree = input.nextInt();
  49.  
  50. System.out.println("Checking..");
  51. //checks users numbers against lock numbers
  52. if(numOne == lock.first()){
  53. if(numTwo == lock.second()){
  54. if(numThree == lock.third()){
  55. open = true;
  56. lock.open();
  57. }
  58. }
  59. }else {
  60. System.out.println("Sorry Wrong Numbers");
  61. --tries;
  62. System.out.println("You have tries " + tries + " left. \n");
  63. if(tries == 0){
  64. System.out.println("Sorry you've been locked out.");
  65. System.out.println("The correct combo was: " + lock.first() + lock.second() + lock.third());
  66. break;
  67. }
  68. }
  69. }while(!open);
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement