java - 继续特定循环(内部与外部)?
问题描述
我在为身份验证类型系统组合的一些循环中有些挣扎。我试图让代码重新提示用户退出,如果他们不输入'q',它将显示他们仍然登录但恢复到主循环并要求再次输入用户名的消息。我在玩 continue 和 break 但到目前为止它不会重复内部 If 语句。任何指导将不胜感激。
package zooauthfinal;
import java.util.Scanner;
import java.io.File;
import java.security.MessageDigest;
public class ZooAuthFinal {
public static void main (String[] args) throws Exception {
//Creates new scanner for user input
Scanner scan = new Scanner(System.in);
//variable to attempts to track login attempts
int attempts = 0;
//While loop to start collecting user credentials for login
while(true) {
System.out.print("Enter username: ");
String user = scan.nextLine(); //reads user input and assigns to variable "user"
System.out.print("Enter password: ");
String original = scan.nextLine(); //reads user input and assigns to variable "original" for original password
//MD5 conversion script
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(original.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
//System.out.println("original:" + original); //Used to check and compare password conversion - shows original user input
//System.out.println("digested:" + sb.toString()); //Used to check and compare password conversion - shows MD5 conversion of original user input
//assigns boolean value to variable "done"
boolean done=false;
//new scanner to read "credentials.txt" file to authenticate user
Scanner file = new Scanner (new File("credentials.txt"));
//While loop to split text file into columns
while(file.hasNextLine ()) {
String record = file.nextLine();
String columns[] = record.split("\t");
//If statement to match value to username
if(columns[0].trim().equals(user)) {
if(columns[1].trim().equals(sb.toString())) {
done=true;
Scanner sc = new Scanner (new File(columns[3].trim() +".txt"));
while(sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
break;
}
}
}
//If statment to start logout procedure, starts when user has been authenticated and user role message has been delivered based on user role
if(done) {
//Instructs user to logout with value 'q'
System.out.println("Type 'q' to logout: ");
String ch = scan.nextLine();
if(ch.toLowerCase().charAt(0) != 'q') {
System.out.println("You are still logged in as " + user +".");
}
//Assigns user input to lowercase value and checks if matches required 'q'
else {
System.out.println("Successfully logged out. Have a nice day!");
}
}
}
//If/else to track number of login attempts (limit 3) and adds 1 to each unsuccessful attempt until 3 attempts, then displays exit protocol
else {
attempts ++;
if(attempts==3) {
System.out.println("Could not verify credentials. Goodbye.\n");
break;
}
//displays login error message on each unsucessful attempt until the third unsuccessful attempt
else {
System.out.println("Invalid username or password, please try again.");
}
}
}
}
解决方案
在 if(done) 块中添加 break 语句作为最后一行。
推荐阅读
- jquery - 如何使用ajax验证并提交验证成功的表单?
- php - 数组项值的总和
- haskell - 是否可以在 Haskell 中引用特定的实例函数?
- sas - SAS宏错误找到的位置参数比定义的多
- python - 如何从训练、测试和验证文件夹中的多个文件夹中拆分和复制文件
- vue.js - 如何通过仅提及父目录名称而不是文件名来导入 index.vue?
- java - 如何在长按或点击时获得点击文本?
- java - Spring Boot @Scheduled fixedDelay 操作
- javascript - NativeScript + javaScript - 动态页面导航
- python-3.x - 在 Windows 上编译 Python 包