簡體   English   中英

如何使密碼的輸入字段變成星號?

[英]How to make input field of password turn into asterisk?

當用戶輸入密碼時,我正在嘗試使輸入字段變為 *****。 但是在這里我遇到了 2 個錯誤,如下所示。 我該如何解決這個問題。 有人可以幫助修改此代碼,使其正常工作。 謝謝你。

//package com.company;

錯誤描述

import java.util.Scanner;
import java.io.Console;
public class Main {
    static void myPassword(){
        String Username = "wisdom";
        String Password = "123";
        boolean granted = false;

        for(int i = 0; i < 3; i++) {
            Scanner input1 = new Scanner(System.in);
            Console console = System.console();
            char[] password = console.readPassword("Enter password");  
            Arrays.fill(password, '*');
            String username = input1.next();

            Scanner input2 = new Scanner(System.in);
            System.out.println("Enter Password : ");
            String password = input2.next();

            if (username.equals(Username) && password.equals(Password)) {
                granted = true;
                break;
            }
            else if (username.equals(Username)) {
                System.out.println("Invalid Password!");
            } else if (password.equals(Password)) {
                System.out.println("Invalid Username!");
            } else {
                System.out.println("Invalid Username & Password!");
            }
        }

        if(granted)
            System.out.println("Access Granted! Welcome!");
        else
            System.out.println("Access Denied! You have reached the maximum number of attempts");
            System.exit(0);
    }

    public static void main(String[] args) {
        myPassword();

    }
}

第一個錯誤:需要先導入Arrays才能使用:

import java.util.Arrays;

第二個錯誤:您已經在這里初始化了password變量:

char[] password = console.readPassword("Enter password"); 

選擇另一個變量名來代替:

String password = input2.next();

我認為這里的錯誤是不言自明的。

對於第一個錯誤: Arrays.fill(password, '*'); ,缺少以下導入語句。

import java.util.Arrays;

對於第二個錯誤: String password = input2.next(); , 變量密碼已經定義在char[] password = console.readPassword("Enter password"); . 只需在此處使用不同的變量名稱。

也許您可以通過答案來理解為什么會出現錯誤。 因此我建議了我的源代碼

import java.util.Scanner;
import java.io.Console;

public class Main {
    static void myPassword(){
        String username = "wisdom";
        String password = "123";
        boolean granted = false;

        Console console = System.console();
        Scanner scanner = new Scanner(System.in);
    
        for(int i = 0; i < 3; i++) {
            System.out.println("Enter username : ");
            // i suggested variable should distinguished by name
            String inputUsername = scanner.next();
        
            char[] passwordChar = console.readPassword("Enter password");  
        
            // this need import java.util.Arrays;
            // and if use this element of passwordChar[] changed *
            //Arrays.fill(passwordChar, '*');
        
            String inputPassword = new String(passwordChar);

           if (username.equals(inputUsername) && password.equals(inputPassword)) {
                granted = true;
                break;
            }
        
            else if (username.equals(inputUsername)) {
                System.out.println("Invalid Password!");
            } else if (password.equals(inputPassword)) {
                System.out.println("Invalid Username!");
            } else {
                System.out.println("Invalid Username & Password!");
            }
        }
        //If you don't need to use scanner
        //You had better close scanner
        scanner.close();
    
        if(granted)
            System.out.println("Access Granted! Welcome!");
        else
            System.out.println("Access Denied! You have reached the maximum number of attempts");
            System.exit(0);
    }

    public static void main(String[] args) {
        myPassword();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM