Java的锁定有关尝试的问题

时间:2018-11-22 12:39:38

标签: java

编写一个名为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
    } 
  } 
}

2 个答案:

答案 0 :(得分:0)

这是我的解决方案。我对Lock做了一些小改动,删除了所有打印内容,并添加了一个成员isFail,以跟踪何时进行了过多的猜测。

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

主要的变化是在GentCPT3类中,其中每个对锁的猜测都是在特定方法中完成的,并且每个锁被调用一次。 请注意,我已经对值进行了硬编码以进行猜测,因为从问题中不清楚它们的来源。也许可以在这里使用Random类来生成两个密钥。

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

答案 1 :(得分:-1)

您的代码中存在的问题是,您永远不会将numAttempts设置为0,这意味着即使您下次使用时已正确解锁,您也将拥有numAttempts。在您的情况下,我认为在成功的情况下适合在Open()中重置计数器。

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