我想编写一个简单的java程序来检查keycode是否与一组条件匹配。
这是我到目前为止所做的:
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
do {
if (key1.length() < 6 || key1.length() > 8) {
System.out.println("must be at least 6 letter and max 8 letter");
return;
}
else {
boolean upper = false;
boolean lower = false;
boolean number = false;
for (char c : key1.toCharArray()) {
if (Character.isUpperCase(c)) {
upper = true;
} else if (Character.isLowerCase(c)) {
lower = true;
} else if (Character.isDigit(c)) {
number = true;
}
}
if (!upper) {
System.out.println("must contain at least one uppercase character");
return;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
return;
} else if (!number) {
System.out.println("must contain at least one number");
return;
} else {
return;
}
}
} while (true);
System.out.println("Input keycode again");
String key2 = keycode.nextLine();
if (key1.equals(key2)) {
System.out.println("keycode matched");
} else {
System.out.println("keycode dont match");
}
提示用户输入密钥代码。
程序首先检查它是否超过6个字符且少于8个字符。
然后检查它是否包含小写,大写和数字。
我希望它允许用户再次输入密码,如果他犯了任何错误而不是重新做一遍。
如果成功,它将要求用户再次输入密钥。如果两个密码不匹配, 允许用户再试一次。在3次尝试失败后,系统将回复'keycode mismatch'。
我需要帮助的部分是允许用户输入密码,如果它不符合要求和密码不匹配。当我输入少于6个字符的密码时,我得到以下输出:
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
答案 0 :(得分:2)
使用循环
boolean flag=false;
while(flag!=true)
{
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
System.out.println("must be at least 6 letter and max 8 letter");
}
else
{
flag=true;
}
}
答案 1 :(得分:0)
您应该将行String key1 = keycode.nextLine();
放在do循环中,以便每次需要时都会查询用户一次。否则,你只是停留在一个循环中,它总是会一次又一次地检查相同的值。
答案 2 :(得分:0)
使用boolean
变量说redo
。在您使用过返回的每个地方,将其替换为redo = true
。而您的while条件应该是while(redo)
,这将持续到redo is true
,用户没有输入正确的数字。
另外,移动input
阅读线: -
String key1 = keycode.nextLine();
在do-while循环中。另外,请在循环中将redo
变量重置为false
,以使其永久保留true
。
String[] keys = new String[2]; // Since you are reading 2 keys, so size = 2
boolean redo = false;
int count = 0; // To maintain the number of keys read. should stop at count = 2
do {
redo = false;
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode No: + " + (count + 1));
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
redo = true;
} else {
/** Your remaining code **/
if (!upper) {
System.out.println("must contain at least one uppercase character");
redo = true;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
redo = true;
} else if (!number) {
System.out.println("must contain at least one number");
redo = true;
} else {
keys[count++] = key1; // Enter a new key in array
}
}
} while (redo || count < 2);
if (keys[0].equals(keys[1])) {
System.out.println("Keys are equal");
}
答案 3 :(得分:0)
boolean redo = false;
do {
redo = false;
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
redo = true;
} else {
/** Your remaining code **/
if (!upper) {
System.out.println("must contain at least one uppercase character");
redo = true;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
redo = true;
} else if (!number) {
System.out.println("must contain at least one number");
redo = true;
}
else{
System.out.println("Input keycode again");
String key2 = keycode.nextLine();
if (key1.equals(key2)) {
System.out.println("keycode matched");
} else {
System.out.println("keycode dont match");
}
}
}
} while (redo);
我设法将代码放在这里,不确定它是否是正确的方法?