top

新手,問題一問。。

one:
package test;
import java.util.Scanner;
public class LibraryCardGeneration {

        public static void main(String[] args) {

                String userName;
                String password;
               
                System.out.println("************************");
                System.out.println(" Welcome to OOP Library");
                System.out.println("************************");
               
                Scanner scanner = new Scanner(System.in);
               
               
                // Read user name from standard input (Task: user name cannot be empty)
                System.out.print("Please set your user name: ");
                userName = scanner.nextLine();
                               
               
                // Read user password from standard input (Task: Ask the user to re-enter the password to reconfirm)
                System.out.print("Please set your password: ");
                password = scanner.nextLine();
                       
               
                // Create a new library card
                LibraryCard newCard = new LibraryCard(userName,password);

                System.out.println("Your library card is generated successfully!");
               
               
        newCard.printLibraryCard();
               
                scanner.close();
       


        }

}

two:
package test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CryptWithMD5 {
   private static MessageDigest md;

   public static String cryptWithMD5(String pass){
    try {
        md = MessageDigest.getInstance("MD5");
        byte[] passBytes = pass.getBytes();
        md.reset();
        byte[] digested = md.digest(passBytes);
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<digested.length;i++){
            sb.append(Integer.toHexString(0xff & digested[i]));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(CryptWithMD5.class.getName()).log(Level.SEVERE, null, ex);
    }
        return null;
   }
}

three:
package test;

public class LibraryCard {
        private String userName;
         private String cryptPassword;
         //private Calendar dateOfApplication;
         //private int borrowedNum;
         
         
         public LibraryCard(String userName2, String password) {
                 userName = userName2;
                 cryptPassword = CryptWithMD5(password);
         }
       
       
        public void printLibraryCard()
         {
                 System.out.println("----------------------------------------------------------");
                 System.out.println("                      OOP Library Card                  ");
                 System.out.println("----------------------------------------------------------");
                 System.out.println("User name              :"+ userName);
                 System.out.println("crypt Password              :"+cryptPassword );
                 
                 
                 

}
}





問題:
1.
   cryptPassword = CryptWithMD5(password);
這句爲什麽無法調用 CryptWithMD5?
2.LibraryCard newCard = new LibraryCard(userName,password);
這個爲什麽顯示錯誤。。


運行結果顯示:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        The method CryptWithMD5(String) is undefined for the type LibraryCard

        at test.LibraryCard.<init>(LibraryCard.java:12)
        at test.LibraryCardGeneration.main(LibraryCardGeneration.java:28)

cryptPassword = CryptWithMD5(password) 沒有定義這個方法,應該是要  cryptPassword = new CryptWithMD5(password) 吧!

TOP