使用Java循环返回到程序的另一部分?
我一直在自学Java,而且我对循环的理解也很深。我知道您可以利用“while”循环来帮助将Java程序带回代码的特定部分。。。但我不知道如何实施 下面是我特别想要的:在完成代码的有效或无效结果后,我希望它返回到提示用户输入密码的代码部分 下面是我的代码副本。。。任何帮助都将不胜感激使用Java循环返回到程序的另一部分?,java,Java,我一直在自学Java,而且我对循环的理解也很深。我知道您可以利用“while”循环来帮助将Java程序带回代码的特定部分。。。但我不知道如何实施 下面是我特别想要的:在完成代码的有效或无效结果后,我希望它返回到提示用户输入密码的代码部分 下面是我的代码副本。。。任何帮助都将不胜感激 import java.util.Scanner; public class Password { public static void main(String[] args) { Scanner
import java.util.Scanner;
public class Password {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Prompts the user to enter a password, and stores password in the string "passwordIn."
System.out.print("Please enter a password to determine if it is valid or not: ");
String passwordIn = input.nextLine();
//Established various boolean checks to be passed in order for code to evaluate each character.
boolean lengthCheck = false;
boolean upperCheck = false;
boolean lowerCheck = false;
boolean digitCheck = false;
boolean specialCheck = false;
boolean check = false;
//The loop will initiate the testing of the string.
for(int i = 0; i < passwordIn.length(); i++) {
//Character pw represents the index!
char pw = passwordIn.charAt(i);
//This verifies that the password meets the length (at least 8 characters) requirement.
if (passwordIn.length() >= 8) {
lengthCheck = true;
}
//This verifies that there is at least one uppercase letter in the password.
if(Character.isUpperCase(pw)) {
upperCheck = true;
}
//This verifies that there is at least one lowercase letter in the password.
if(Character.isLowerCase(pw)) {
lowerCheck = true;
}
//This verifies that there is at least one digit in the password.
if(Character.isDigit(pw)) {
digitCheck = true;
}
//This verifies that there is at least one character that is not a letter or a number within the password.
if(!Character.isLetter(pw) && !Character.isDigit(pw)) {
specialCheck = true;
}
}
//Verifies the results of the loop to ensure that all checks are true.
if(upperCheck == true && lowerCheck == true && digitCheck == true && lengthCheck == true && specialCheck == true) {
check = true;
}
// Uses boolean to determine if password is valid.
if (check == true) {
System.out.print("\nThe password you entered was: " + passwordIn);
System.out.print("\nVerdict: " + "\t" + "Valid");
}
else {
System.out.print("\nThe password you entered was: " + passwordIn);
System.out.print("\nVerdict: " + "\t" + "Invalid");
}
}
}
import java.util.Scanner;
公共类密码{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//提示用户输入密码,并将密码存储在字符串“passwordIn”中
System.out.print(“请输入密码以确定其是否有效:”);
字符串passwordIn=input.nextLine();
//建立了要传递的各种布尔检查,以便代码计算每个字符。
布尔长度检查=假;
布尔值上限检查=false;
布尔lowerCheck=false;
布尔digitCheck=false;
布尔特殊检查=假;
布尔检查=假;
//循环将启动字符串的测试。
for(int i=0;i=8){
lengthCheck=true;
}
//这将验证密码中是否至少有一个大写字母。
if(字符.isUpperCase(pw)){
upperCheck=true;
}
//这将验证密码中是否至少有一个小写字母。
if(字符isLowerCase(pw)){
lowerCheck=true;
}
//这将验证密码中是否至少有一个数字。
if(字符isDigit(pw)){
digitCheck=true;
}
//这将验证密码中是否至少有一个字符不是字母或数字。
if(!Character.islitter(pw)和&!Character.isDigit(pw)){
specialCheck=true;
}
}
//验证循环的结果,以确保所有检查均为真。
如果(大写检查==true&&lowerCheck==true&&digitCheck==true&&lengthCheck==true&&specialCheck==true){
检查=正确;
}
//使用布尔值确定密码是否有效。
如果(检查==true){
System.out.print(“\n您输入的密码为:“+passwordIn”);
System.out.print(“\n更正:“+”\t“+”有效”);
}
否则{
System.out.print(“\n您输入的密码为:“+passwordIn”);
System.out.print(“\n更正:+”\t“+”无效”);
}
}
}
对于类似的内容,通常使用测试布尔值,而
boolean check = false;
//The loop will initiate the testing of the string.
while(!check) {
但是,您必须移动从用户处读取密码的语句才能执行任何有用的操作
boolean check = false;
//The loop will initiate the testing of the string.
while(!check) {
//Prompts the user to enter a password, and stores password in the string "passwordIn."
System.out.print("Please enter a password to determine if it is valid or not: ");
String passwordIn = input.nextLine();
// ... testing...
}
您可以使用
do while
循环来执行此操作,如下所示
do{
// logic to validate
} while(condition); // Your break condition here
这将执行一次逻辑,然后检查断路情况。如果它是true
,则它将再次执行该逻辑。这将持续到遇到中断条件或程序终止