简体   繁体   中英

java. Lock problem with an issue regarding attempts

Writing a program with a class named Lock that user inputs attmpt to open. If they guess the correct combination, they move on to the second lock. The user gets 3 attempts to correctly solve each lock. If guess incorectly 3 times in a row for each lock, they are met with an alarm So far I have my program doing mostly what I want it to do, however, I only get one attempt per lock and when the user guesses the combo incorrectly, the program just goes on to lock number 2. Here is my code:

import java.io.*;
public class GentCPT3
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in)); 

      System.out.println("Enter key");
      int key1 = Integer.parseInt(objReader.readLine()); // set to 111  

      System.out.println("Enter key2");
      int key2 = Integer.parseInt(objReader.readLine()); // set to 222

      Lock lock1 = new Lock (key1);

      Lock lock2 = new Lock (key2);

      System.out.println(lock1.isOpen); // prints false

      lock1.close();
      lock2.close();
      lock1.open(111); // opens lock1
      lock2.open(222); // opens lock2
      lock1.open(111);
      lock2.open(222);
      lock1.open(111);
      lock2.open(222);

    }
  }
class Lock //Initializing class
{
  //Initializing variables
  boolean isOpen;
  int key; 
  int numAttempts = 0;

  Lock(int key) 
  {
    isOpen = false; 
    this.key = key;
  } 

  public void close()//for incorrect combo 
  {
    isOpen = false;
  } 
  public void open(int key)//for correct combo
  { 
    if(this.key == key) 
    {
      System.out.println("Opened");
      isOpen = true;
    } 
    else if(!isOpen) 
    {
      numAttempts++;
    } 
    if(numAttempts == 3) 
    {
      System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
    } 
  } 
}

Here is my solution. I did some minor changes to Lock where I removed any printing and added a member, isFail , to keep track of when too many guesses has been done.

class Lock {
  boolean isOpen;
  boolean isFail;
  int key; 
  int numAttempts = 0;

  Lock(int key) {
    isOpen = false; 
    isFail = false;
    this.key = key;
  } 

  public void open(int key) { 
    if (isOpen) {
      return;
    }

    if(this.key == key) {
      isOpen = true;
    } else {
      numAttempts++;
      isFail = numAttempts == 3;
    }  
  } 
}

The major changes is in the GentCPT3 class where each guess for a lock is done in specific method and that method is called once for each lock. Note that I have hardcoded the values to guess since it is not clear from the question where they come from. Maybe using the Random class here to generate two keys would be an option.

import java.io.*;
public class GentCPT3 {
  public static void main (String[] args) throws IOException {
    BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in)); 

    if (guess(objReader,  new Lock (3))) {
      guess(objReader, new Lock(7));
    }
    objReader.close();
  }

  private static boolean guess(BufferedReader objReader, Lock lock) throws IOException {
    while (true) {
      System.out.println("Guess value");
      int key1 = Integer.parseInt(objReader.readLine());
      lock.open(key1);
      if (lock.isOpen) {
        System.out.println("Success, you have unlocked the lock");
        return true;
      } else if (lock.isFail) {
        System.out.println("Alarm, you have failed to unlocked the lock");
        return false;
      } else {
        System.out.println("Incorrect guess, try again");
      }
    }
  }
}

The problem that you have in your code is that you are never set the numAttempts to 0, this means that eventhough you have correctly unlocked the next time you used it you will have numAttempts. In your case I think it fits to reset the counter in Open() when it is success.

import java.io.*;
public class GentCPT3
{
  public static void main (String[] args) throws IOException
  {
    BufferedReader objReader = new BufferedReader(new InputStreamReader (System.in)); 

      System.out.println("Enter key");
      int key1 = Integer.parseInt(objReader.readLine()); // set to 111  

      System.out.println("Enter key2");
      int key2 = Integer.parseInt(objReader.readLine()); // set to 222

      Lock lock1 = new Lock (key1);

      Lock lock2 = new Lock (key2);

      System.out.println(lock1.isOpen); // prints false

      lock1.close();
      lock2.close();
      lock1.open(111); // opens lock1
      lock2.open(222); // opens lock2
      lock1.open(111);
      lock2.open(222);
      lock1.open(111);
      lock2.open(222);

    }
  }
class Lock //Initializing class
{
  //Initializing variables
  boolean isOpen;
  int key; 
  int numAttempts = 0;

  Lock(int key) 
  {
    isOpen = false; 
    this.key = key;
  } 

  public void close()//for incorrect combo 
  {
    isOpen = false;
  } 
  public void open(int key)//for correct combo
  { 
    if(this.key == key) 
    {
      System.out.println("Opened");
      isOpen = true;
      numAttempts = 0;
    } 
    else if(!isOpen) 
    {
      numAttempts++;
    } 
    if(numAttempts == 3) 
    {
      System.out.println("ALARM");//prints alarm when the combo is incorrect 3 times
    } 
  } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM