首页 > 解决方案 > 爪哇。锁定问题与尝试有关的问题

问题描述

使用名为 Lock 的类编写程序,用户输入 attmpt 以打开该类。如果他们猜对了密码,他们就会进入第二把锁。用户有 3 次尝试正确解决每个锁。如果每把锁连续猜错 3 次,他们就会收到警报 到目前为止,我的程序主要做我想做的事情,但是,每个锁我只能尝试一次,并且当用户猜错组合时,程序继续锁定 2 号。这是我的代码:

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
    } 
  } 
}

标签: java

解决方案


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");
      }
    }
  }
}

推荐阅读