Java I';我的代码中有一个运行时错误。当我输入字母时,只有布尔有效值变为真
此代码将输入密码并检查密码是否包含字母和数字。如果密码不同时具有两个寄存器,则将继续 当我输入字母和数字时,寄存器是完整的;当我只输入数字时,寄存器继续;当我只输入字母时,寄存器是完整的,不包含数字Java I';我的代码中有一个运行时错误。当我输入字母时,只有布尔有效值变为真,java,Java,此代码将输入密码并检查密码是否包含字母和数字。如果密码不同时具有两个寄存器,则将继续 当我输入字母和数字时,寄存器是完整的;当我只输入数字时,寄存器继续;当我只输入字母时,寄存器是完整的,不包含数字 import java.io.*; public class Sample { static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); public static void main
import java.io.*;
public class Sample {
static BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
public static void main (String[] args)throws Exception {
boolean valid = false;
boolean alphaCheck = false;
boolean numCheck = false;
char[] alpha = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] num = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
do {
System.out.println("Password must contain Letters and Numbers Only. (abc../ABC.., 123..)");
System.out.println("Special Characters are not Allowed.");
System.out.print("Register Password: ");
String password = dataIn.readLine();
char[] passwordToArray = password.toUpperCase().toCharArray();
// Check if password contains numbers
for(char x: passwordToArray){
for(char y: num){
if(x==y){
numCheck = true;
break;
}
}
}
// Check if password contains Letters
for(char x: passwordToArray){
for(char y: alpha){
if(x==y){
alphaCheck = true;
break;
}
}
}
if(!numCheck){
System.out.println("No Numbers Found.");
}
if(!alphaCheck){
System.out.println("No Letters Found.");
}
// Check if password contains both Alphabets and Numbers
// if false Continue Register
if((numCheck)&&(alphaCheck)){
System.out.println("Register Complete");
valid = true;
}else{
System.out.println("Register Failed. Please Try again");
}
}while (!valid) ;
}
}
这里的问题是,在do while循环中,您从未将布尔值设置为其原始状态(应该为false) 假设您插入了一个只包含数字的密码,因为密码无效,所以第一次不会传递该密码,但是布尔值numCheck在以后的每次迭代中都将为true,当然,如果您只输入字符,则同样适用 该程序将在三种情况下传递插入的密码
import java.io.*;
public class Sample {
static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
boolean valid = false;
char[] alpha = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] num = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
do {
boolean alphaCheck = false;
boolean numCheck = false;
System.out.println("Password must contain Letters and Numbers Only. (abc../ABC.., 123..)");
System.out.println("Special Characters are not Allowed.");
System.out.print("Register Password: ");
String password = dataIn.readLine();
char[] passwordToArray = password.toUpperCase().toCharArray();
System.out.println(passwordToArray);
// Check if password contains numbers
for (char x : passwordToArray) {
for (char y : num) {
if (x == y) {
numCheck = true;
break;
}
}
}
// Check if password contains Letters
for (char x : passwordToArray) {
for (char y : alpha) {
if (x == y) {
System.out.println(x + " == " + y);
alphaCheck = true;
break;
}
}
}
if (!numCheck) {
System.out.println("No Numbers Found.");
}
if (!alphaCheck) {
System.out.println("No Letters Found.");
}
// Check if password contains both Alphabets and Numbers
// if false Continue Register
if (numCheck && alphaCheck) {
System.out.println("Register Complete");
valid = true;
} else {
System.out.println("Register Failed. Please Try again");
}
} while (!valid);
}
}
在循环之前声明(并初始化)一次验证变量,但可能会多次循环。首先是字母,然后是数字。或者按相反的顺序。无论如何,您应该重置循环体中的那些sentinel值。差不多
boolean valid = false;
boolean alphaCheck = false;
boolean numCheck = false;
char[] alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] num = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
do {
valid = alphaCheck = numCheck = false;
我还建议您编写一个函数来检查允许值数组中是否存在该值。这可以使代码的其余部分更容易阅读
private static boolean contains(char[] arr, char ch) {
for (char v : arr) {
if (ch == v) {
return true;
}
}
return false;
}
和,我们可以通过使用。像
这是家庭作业吗?你在使用正则表达式和/或集合或第三方库等方面有限制吗?我之所以这么问,是因为有比在arraysno上迭代更好的解决方案。这不是我的家庭作业。我只是想尝试一下我使用数组循环和if-else的技巧。我只想尝试使用for循环和if-else验证密码。如果您知道解决方案,请提供帮助我无法理解为什么我有运行时错误粘贴堆栈跟踪。我只想了解为什么我有运行时错误。如果您粘贴堆栈跟踪,它将帮助我们调试问题。堆栈跟踪包含引发RunTimeException的行。顺便说一下,您的代码效率非常低。所有布尔值都设置为false。我不知道错误是在numCheck中还是((numCheck)&&(alphaCheck))不是,do while继续使用错误的布尔值。请将alphaCheck和numCheck放在do while循环中,它将工作。我现在明白了,谢谢您的帮助。num&alphaCheck应该在do while中,因为每次我输入密码时,如果密码无效,布尔值应该保持为false。非常感谢你的帮助。这是我第一次来这里。这次我不想使用函数/方法/类。我只想用数组、循环等来测试我的基本技能。
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
char[] num = "1234567890".toCharArray();