Java 谁能帮我解决这个程序的错误?非常感谢你。对不起,如果我的英语不好
我是java的初学者,下面是我的代码Java 谁能帮我解决这个程序的错误?非常感谢你。对不起,如果我的英语不好,java,compiler-errors,Java,Compiler Errors,我是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 = f
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);
}
}
import java.io.*;
导入java.util.*;
公共类校验密码{
公共静态无效密码验证器(字符串密码){
int n=password.length();
布尔hasDigit=false,specialChar=false,hasLetter=false;
Set Set=new HashSet(Arrays.asList(“!”、“@”、“#”、“$”、“%”、“^'、“&'、“*”、“(“,”)、“-”、“+”);
for(char i:password.toCharArray()){
if(字符(i))
hasLetter=true;
if(字符isDigit(i))
hasDigit=true;
如果(集合包含(i))
specialChar=真;
}
仅限int数字=0;
int onlyletter=0;
如果(n>=1){
对于(i=0;i
Visual Studio代码显示以下错误:
线程“main”java.lang中出现异常。错误:未解决的编译问题:我无法解析为变量
我无法解析为变量
我无法解析为变量
我无法解析为变量
在CheckPassword.PasswordValidator(CheckPassword.java:21)
位于CheckPassword.main(CheckPassword.java:56)
谁能帮我解决这个错误?在第21行(对于带有
i
的循环),您没有声明类型。用修复(int i=0;i
。应该可以解决这个问题。您忘记将i声明为“int”变量。在第20行,您在编写之前忘记了编写int(i=0;iimport 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);
}
}
import java.io.*;
导入java.util.*;
公共类校验密码{
公共静态无效密码验证器(字符串密码){
int n=password.length();
布尔hasDigit=false,specialChar=false,hasLetter=false;
Set Set=new HashSet(Arrays.asList(“!”、“@”、“#”、“$”、“%”、“^'、“&'、“*”、“(“,”)、“-”、“+”);
for(char i:password.toCharArray()){
if(字符(i))
hasLetter=true;
if(字符isDigit(i))
hasDigit=true;
如果(集合包含(i))
specialChar=真;
}
仅限int数字=0;
int onlyletter=0;
如果(n>=1){
对于(int i=0;i
第二个for循环中的i
需要声明为新的int
:for(int i=0;i
如果正确解决了您的问题,请接受其中一个答案。您有两天的时间来完成此操作。