我正在制作一个简单的程序,要求您输入密码才能启动该程序。当我的用户输入错误的密码时,他们会收到“访问被拒绝”警告。我使用If / Else语句来实现。我想要做的是,如果输入的密码错误,请重新运行我的程序,因为如果输入的密码错误,他们将无法再次输入控制台。
这是我的工作区:
import java.util.Scanner;
public class PasswordProtected {
public static void main (String args[]){
Scanner Password = new Scanner (System.in);
String mainpassword, userInput;
mainpassword = ("Jacob");
System.out.println("Please enter the password to continue.");
userInput = Password.nextLine();
System.out.println("Verifying Password");
if (userInput.equals(mainpassword)){
System.out.println("Access Granted");
System.out.println("Welcome!");
}else{
System.out.println("Access Denied");
}
}
}
我确实意识到我可以一遍又一遍地复制这样的东西,但是,浪费的空间并不是无限的。
System.out.println("Please enter the password to continue.");
userInput = Password.nextLine();
System.out.println("Verifying Password");
if (userInput.equals(mainpassword)){
System.out.println("Access Granted");
System.out.println("Welcome!");
}else{
System.out.println("Access Denied");
}
}
请注意,我是编程新手,可能需要一些额外的帮助。
如果触发了Else语句,该如何完全重新启动程序,而无需再次手动单击运行按钮?
您不需要重新启动程序。如果不正确,请使用循环再次询问密码。例如,在带有while语句的半伪代码中:
userInput = input.nextLine();
while ( !userInput.equals(mainpassword) ){
userInput = input.nextLine();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句