Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使代码更简单(Java,简单的字符串输入,比如它是否正确)_Java - Fatal编程技术网

如何使代码更简单(Java,简单的字符串输入,比如它是否正确)

如何使代码更简单(Java,简单的字符串输入,比如它是否正确),java,Java,这是我想出的帽子: public static void main(String[] args){ String pass = "admin"; int derp = 0; Scanner input = new Scanner(System.in); System.out.print("Insert the admin pass: "); String test = input.nextLine(); if (pass.equals(test))

这是我想出的帽子:

public static void main(String[] args){
    String pass = "admin";
    int derp = 0;
    Scanner input = new Scanner(System.in);
    System.out.print("Insert the admin pass: ");
    String test = input.nextLine();
    if (pass.equals(test)){
        System.out.print("Access granted");
    }else{
        int counter = 3;
        while (counter > 1){
            counter--;
            System.out.print("You have "+counter+" try(es): ");
            test = input.nextLine();
            if (pass.equals(test)){
                System.out.print("Access granted");
                counter -= 2;
            }else{
                derp++;
            }
        }
    }
    if (derp == 2){
        System.out.print("Access denied");
    }
}
我怎么能让它说“拒绝访问”?您可能会看到,为了解决这个问题,我刚刚创建了一个名为“derp”的int,如果用户失败三次,我应该如何删除它并仍然说“拒绝访问”


谢谢。另外,如果您认为还有更多的地方需要改进,我将不胜感激。

我会做如下事情

boolean success = false;
int times = 0;
do {
   input.nextLine();
   if (condition) {
     success = true;
   } else {
     count++;
   }
} while(!success && times < MAX_TIMES);
if (success) {
  print("SUCCESS!!");
} else {
  print("FAIL");
}
boolean success=false;
整数倍=0;
做{
input.nextLine();
如果(条件){
成功=真实;
}否则{
计数++;
}
}而(!成功次数<最大次数);
如果(成功){
打印(“成功!!”;
}否则{
打印(“失败”);
}

我将使用以下代码:

            boolean flag = true;
            int counter = 3;
            while(flag)
            {
                if (pass.equals(test))
                {
                    System.out.print("Access granted");
                    break;  // Permission granted so out of the Loop
                }
                else if(counter==0)
                {
                    flag = false;
                    break;
                }

                System.out.print("You have "+counter+" try(es): ");
                test = input.nextLine();
                counter--;
            }

            if(!flag)
            {
                System.out.print("Access denied");
            }
import java.util.Scanner;
公开课考试
{
公共静态void main(字符串[]args)
{
字符串pass=“admin”;
扫描仪输入=新扫描仪(System.in);

对于(int i=0;i使用
for
循环和
break
语句,可以执行以下操作

public static void main(String[] args) {
    String pass = "admin";
    Scanner input = new Scanner(System.in);
    System.out.print("Insert the admin pass: ");
    String test = input.nextLine();
    if (pass.equals(test)) {
        System.out.print("Access granted");
    } else {

        for (int i = 1; i < 3; i++) {
            System.out.print("You have " + i + " try(es): ");
            test = input.nextLine();
            if (pass.equals(test)) {
                System.out.print("Access granted");
                break;
            } else if (i == 2) {
                System.out.print("Access denied");
            }
        }

    }
}
publicstaticvoidmain(字符串[]args){
字符串pass=“admin”;
扫描仪输入=新扫描仪(System.in);
系统输出打印(“插入管理员通行证:”);
字符串测试=input.nextLine();
如果(通过等于(测试)){
系统输出打印(“授权访问”);
}否则{
对于(int i=1;i<3;i++){
System.out.print(“您有“+i+”try:”;
test=input.nextLine();
如果(通过等于(测试)){
系统输出打印(“授权访问”);
打破
}else如果(i==2){
系统输出打印(“访问被拒绝”);
}
}
}
}
试试看:

public static void main(String[] args) {
    String pass = "admin";
    int maxTry = 3;
    boolean success = false;
    Scanner input = new Scanner(System.in);
    System.out.print("Insert the admin pass: ");
    String test = input.nextLine();
    while(maxTry>0){
        maxTry--;
        if (!pass.equals(test)) {
            System.out.print("You have " + maxTry + " try(es): ");
            test = input.nextLine();
        } else {
            success = true;
            break;
        }
    }
    if(success){
        System.out.println("Access granted");
    }else{
        System.out.println("Access denied");
    }
}

如果我要写这段代码,我会这样写:

import java.util.Scanner;

public class AccessPoint
{
  private Scanner scanner;

  public AccessPoint()
  {
    scanner = new Scanner(System.in);

    if (tryAccessForTimes(3))
    {
      allowAccess();
    }
    else
    {
      denyAccess();
    }

    scanner.close();
  }

  private boolean tryAccessForTimes(int times)
  {
    boolean accessAllowed = false;

    for (int tryIndex = 1; tryIndex <= times && !accessAllowed; tryIndex++)
    {
      if (getAdminPassword().equals(getUserPassword()))
      {
        accessAllowed = true;
      }
      else
      {
        printNumberOfTriesLeft(times, tryIndex);
      }
    }

    return accessAllowed;
  }

  private void printNumberOfTriesLeft(int times, int tryIndex)
  {
    int triesLeft = times - tryIndex;

    if (triesLeft != 0)
    {
      System.out.println("You have " + triesLeft
          + (triesLeft == 1 ? " try" : " tries") + " left.");
    }
  }

  private String getUserPassword()
  {
    System.out.print("Enter Password: ");
    String password = scanner.nextLine();

    return password;
  }

  private String getAdminPassword()
  {
    return "admin";
  }

  private void allowAccess()
  {
    System.out.println("Access Granted.");
  }

  private void denyAccess()
  {
    System.out.println("Access Denied.");
  }

  public static void main(String[] args)
  {
    new AccessPoint();
  }
}
import java.util.Scanner;
公共类访问点
{
私人扫描仪;
公共访问点()
{
扫描仪=新扫描仪(System.in);
if(tryAccessForTimes(3))
{
allowAccess();
}
其他的
{
denyAccess();
}
scanner.close();
}
专用布尔值tryAccessForTimes(整数次)
{
布尔accessAllowed=false;

对于(int-tryIndex=1;tryIndex)您必须引入任何标志才能做出决定。尝试计数必须像3..2..1…结束一样减少,因此更改条件它工作起来像个符咒。但我不理解为什么“标志”是while的条件。我知道while必须有一个条件,但它只是为了这个条件吗?要成为一个条件使它运行吗?在解决方案中要做两件事。首先使while条件为true,然后打印拒绝访问的消息检查。这太复杂了。我不明白。但是感谢您的时间
import java.util.Scanner;

public class AccessPoint
{
  private Scanner scanner;

  public AccessPoint()
  {
    scanner = new Scanner(System.in);

    if (tryAccessForTimes(3))
    {
      allowAccess();
    }
    else
    {
      denyAccess();
    }

    scanner.close();
  }

  private boolean tryAccessForTimes(int times)
  {
    boolean accessAllowed = false;

    for (int tryIndex = 1; tryIndex <= times && !accessAllowed; tryIndex++)
    {
      if (getAdminPassword().equals(getUserPassword()))
      {
        accessAllowed = true;
      }
      else
      {
        printNumberOfTriesLeft(times, tryIndex);
      }
    }

    return accessAllowed;
  }

  private void printNumberOfTriesLeft(int times, int tryIndex)
  {
    int triesLeft = times - tryIndex;

    if (triesLeft != 0)
    {
      System.out.println("You have " + triesLeft
          + (triesLeft == 1 ? " try" : " tries") + " left.");
    }
  }

  private String getUserPassword()
  {
    System.out.print("Enter Password: ");
    String password = scanner.nextLine();

    return password;
  }

  private String getAdminPassword()
  {
    return "admin";
  }

  private void allowAccess()
  {
    System.out.println("Access Granted.");
  }

  private void denyAccess()
  {
    System.out.println("Access Denied.");
  }

  public static void main(String[] args)
  {
    new AccessPoint();
  }
}