java - 谁能帮我解决这个程序的这个错误?非常感谢。对不起,如果我的英语不好
问题描述
我是java的初学者,这是我的代码
import java.io.*;
import java.util.*;
public class CheckPassword {
public static void PasswordValidator(String password) {
int n = password.length();
boolean hasDigit = false, specialChar = false, hasLetter = false;
Set<Character> set = new HashSet<Character>(Arrays.asList('!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+'));
for (char i : password.toCharArray()) {
if (Character.isLetter(i))
hasLetter = true;
if (Character.isDigit(i))
hasDigit = true;
if (set.contains(i))
specialChar = true;
}
int onlydigit = 0;
int onlyletter = 0;
if (n >= 1) {
for (i = 0; i < n; i++) {
char x = password.charAt(i);
if (Character.isDigit(x)) {
onlydigit++;
}
if (Character.isLetter(x)) {
onlyletter++;
}
}
}
System.out.print("Your password " + '"' + password + '"' + " is ");
if (n < 8) {
if (n == onlydigit) {
System.out.println("very weak");
} else {
System.out.println("weak");
}
} else {
if ((hasDigit == true) && (hasLetter == true)) {
if ((onlydigit + onlyletter) == n) {
System.out.println("strong");
} else {
if (specialChar == true) {
System.out.println("very strong");
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("ENTER YOUR PASSWORD: ");
String password = sc.nextLine();
PasswordValidator(password);
}
}
Visual Studio Code 给我这个错误:
线程“主”java.lang.Error 中的异常:未解决的编译问题:
我无法解析为变量
我无法解析为变量
我无法解析为变量
我无法解析为
CheckPassword.PasswordValidator 的变量(CheckPassword .java:21)
在 CheckPassword.main(CheckPassword.java:56)
谁能帮我解决这个错误?
解决方案
您忘记将 i 声明为“int”变量。在第 20 行,你在写 (i = 0; i < n; i++) 之前忘记写 int。我在下面的代码中修复了该行。
import java.io.*;
import java.util.*;
public class CheckPassword {
public static void PasswordValidator(String password) {
int n = password.length();
boolean hasDigit = false, specialChar = false, hasLetter = false;
Set<Character> set = new HashSet<Character>(Arrays.asList('!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+'));
for (char i : password.toCharArray()) {
if (Character.isLetter(i))
hasLetter = true;
if (Character.isDigit(i))
hasDigit = true;
if (set.contains(i))
specialChar = true;
}
int onlydigit = 0;
int onlyletter = 0;
if (n >= 1) {
for (int i = 0; i < n; i++) {
char x = password.charAt(i);
if (Character.isDigit(x)) {
onlydigit++;
}
if (Character.isLetter(x)) {
onlyletter++;
}
}
}
System.out.print("Your password " + '"' + password + '"' + " is ");
if (n < 8) {
if (n == onlydigit) {
System.out.println("very weak");
} else {
System.out.println("weak");
}
} else {
if ((hasDigit == true) && (hasLetter == true)) {
if ((onlydigit + onlyletter) == n) {
System.out.println("strong");
} else {
if (specialChar == true) {
System.out.println("very strong");
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("ENTER YOUR PASSWORD: ");
String password = sc.nextLine();
PasswordValidator(password);
}
}
推荐阅读
- r - 将大型数据集分组为特定标准
- scala - Scala中lambda演算中的布尔逻辑
- vba - 使用 VBA OpenTextFile 写入替换文本的代码使文件损坏
- arrays - 在 Binary Max Heap 中插入新元素
- javascript - 取消隐藏菜单项
- javascript - 使用多个层的 OpenLayers 4.6.5 的性能限制
- python - 将指针从 Python 传输到 dll 并取回修改后的值
- python - 在 python-docx 中替换单词时保留样式
- python - 烧瓶表单重定向到同一页面
- sorting - ElasticSearch 深度嵌套排序/评分