首页 > 解决方案 > 谁能帮我解决这个程序的这个错误?非常感谢。对不起,如果我的英语不好

问题描述

我是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)

谁能帮我解决这个错误?

标签: javacompiler-errors

解决方案


您忘记将 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);
    }
}

推荐阅读