循环时的Java密码

时间:2017-03-20 19:10:28

标签: java loops while-loop passwords

好吧所以我正在进行一项任务,我需要用户输入一个密码(芝麻),如果答案不正确,它会循环。问题是我无法分配密码变量,所以我可以这样做。这是我的代码。

import java.util.Scanner;
public class passCheck {

    public static void main(String[] args) {
    String pass = "thing";  
    while(pass != "sesame"){
        System.out.println("The correct password is sesame. Please enter that.");
        Scanner input = new Scanner(System.in);
        String password = input.nextLine();

    }

    }
}

5 个答案:

答案 0 :(得分:3)

两件事:

首先,最重要的是,您必须将下一行的输入分配给变量pass

String pass = ...;
...
pass = input.nextLine();

其次,您应该与equals进行字符串比较。参考比较!=(或==)可能有效,但无法保证。所以while条件应该是:

while(!"sesame".equals(pass)){ ...

运算符!===对对象引用进行操作,适用于比较对象标识。使用字符串它们可能会起作用,因为JVM在String上对某些字符串使用相同的实例。所以无法保证,

例如:

"bla" != new String("bla")

因此,字符串应与equals进行比较。另外,如果要比较的字符串是已知的(如在"sesame"中),建议先将其放在第一位,因为它永远不会是null,这可能是变量的情况。

例如:

"sesame".equals(pass) 
即使NullPointerExceptionpass

也不会抛出null

所以最后你的main应该是

public static void main(String[] args) {
  String pass = "thing";   
  while(!"sesame".equals(pass)){ //comparison with equals
    System.out.println("The correct password is sesame. Please enter that.");
    Scanner input = new Scanner(System.in);
    pass = input.nextLine(); //assign result to pass

}

最后需要注意的是,有一个Console类更适合读取密码,因为密码不会打印到终端:

Console cons = System.console();
char[] passwd = cons.readPassword("[%s]", "Password:"));

但请记住,System.console()可能会返回null,如果您在IDE中使用运行程序运行代码,则可能会发生这种情况。

答案 1 :(得分:2)

String必须与equals()方法进行比较,而不是与比较对象标识的==进行比较。

替换

while(pass != "sesame"){

通过

  while(!"sesame".equals(pass)){

并替换

   String password = input.nextLine();

通过

  pass = input.nextLine();

您不必引入额外的变量来存储密码,否则您将永远循环,因为pass永远不会被修改。

答案 2 :(得分:2)

您的代码存在一些问题。首先,每次创建一个新变量,而实际检查的旧变量永远不会改变,从而创建一个无限循环,循环中的pass = input.nextLine();应该是String password = input.nextLine();而不是!=。您的下一个问题是您正在错误地比较字符串,您无法使用.equals()来比较Java中需要使用<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>MessangerWithoutMaven</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.manish.jax_rs.Messanger</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/webapi/*</url-pattern> </servlet-mapping> </web-app> 方法的字符串。我会留给你研究原因,因为你显然是在学习语言,应该自己去发现。最后,您应该在循环之前放置Scanner声明,每次用户输入错误的密码时,都无法继续重新创建它。祝你好运!

答案 3 :(得分:1)

您需要重新分配您在while循环中检查的变量,而不是创建一个新变量。

String pass = "";  
while(!pass.equals("sesame")){
    System.out.println("The correct password is sesame. Please enter that.");
    Scanner input = new Scanner(System.in);
    pass = input.nextLine();
}

答案 4 :(得分:0)

在java中我们不能通过运算符比较两个字符串!=或==我们使用equal()或!equal()函数来使代码工作只需替换这两行:

while(pass != "sesame")

by:

while(!pass.equals("sesame") )

和:

String password = input.nextLine();

由:

pass = input.nextLine();

因为如果用户输入(在变量:pass中)输入的值是正确的,那么每次变量通过时都会测试相等的密码。 最后你会得到这个代码:

public static void main(String[] args) {
    String pass = "";  
    while(!pass.equals("sesame") ){
        System.out.println("The correct password is sesame.Please enter that.");
        Scanner input = new Scanner(System.in);
        pass = input.nextLine();

    }
}