Java 我怎样才能使用;“边做边做”;在这里指挥?

Java 我怎样才能使用;“边做边做”;在这里指挥?,java,Java,我正在学习java,下面是我的代码 import java.util.Scanner; public class Application { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter Password"); String value = input.nextL

我正在学习java,下面是我的代码

import java.util.Scanner;


public class Application {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.println("Enter Password");
        String value = input.nextLine();


        if(value.equals("mycat")){
            System.out.println("Password Accepted!");
            System.out.println("My Personal Number is: ");
            System.out.println("blahblah");
        }
        else 
            System.out.println("Incorrect Password!");


        }


    }
我想继续说不正确的密码,并允许输入另一个,而不是停止。基本上我想要循环,但每次我这样做时,我都得不到我想要的结果。

考虑使用一个函数来处理密码获取:

String getPassword(/*you might need to pass input as a parameter*/)
{
    while (true){ /*an infinite loop*/
        String value = input.nextLine();
        if (value.equals("mycat")){
            return value;
        }
        System.out.println("Incorrect Password!");
    }
}
这将帮助您在对
getPassword
的调用者进行最小更改的情况下扩展程序:

  • 远离硬编码
    “mycat”

  • 使用为密码建模而设计的特殊对象<代码>字符串不安全,因为数据位于连续块中

  • 引入最大数量的试验,以及可能的延迟循环


  • 将代码括在Try-Catch块中。

    Java中有两个不同的while循环

    do {
        //Code here
    } while (/*Condition here */)
    

    第一个在括号中的代码执行后测试条件,第二个在执行之前测试。 这意味着使用
    do{}while()

    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        String value;
    
        do{
            System.out.println("Enter Password");
            value = input.nextLine();
    
            if(value.equals("mycat")){
                System.out.println("Password Accepted!");
                System.out.println("My Personal Number is: ");
                System.out.println("blahblah");
            }
            else 
                System.out.println("Incorrect Password!");
    
        } while (!value.equals("mycat"))
    
    }
    

    像这样的事情可能是你想要的

    “但每次我这样做时,我都得不到我想要的结果。”请发布这些尝试。你需要休息一下,或者在你的if语句中做些什么。只要密码是mycat,它就会运行…嘿,Rajesh,谢谢你的帮助。我删除了:中的“!”感叹号,同时(!value.equals(“mycat”)和循环停止。这就是我错过的。你能解释一下为什么“!”会造成这种差异吗?“input.close();”的工作是什么。我删除了它,它也没有做任何更改。你说的包含在Try-Catch块中是什么意思?对不起,我是新手。好吧,我明白一件事,你为什么用“!”。这实际上意味着在条件不正确时保持循环。我编辑代码以显示何时需要打印它:如果你在循环中,那么密码是错误的,如果你在循环后,那么密码是正确的。非常感谢。所以不需要“do”就可以完成。是的,只要把“do”放在第一个System.out.println之前,就完成了。是的,就是这样。谢谢你的帮助!
    do {
        //Code here
    } while (/*Condition here */)
    
    while (/*Condition here */) {
        //Code here
    }
    
    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        String value;
    
        do{
            System.out.println("Enter Password");
            value = input.nextLine();
    
            if(value.equals("mycat")){
                System.out.println("Password Accepted!");
                System.out.println("My Personal Number is: ");
                System.out.println("blahblah");
            }
            else 
                System.out.println("Incorrect Password!");
    
        } while (!value.equals("mycat"))
    
    }