我试图创建一个购物车。我知道登录系统有问题。我试图使用hashmap来保持用户的密码,然后通过另一个函数验证它们是否存在配对。问题是该函数具有"非静态方法containsKey(Object),并且我不能将verify_user方法与hmap一起使用。我真的很感激一些帮助。提前谢谢你:D!
package shoppingcart;
import java.util.HashMap;
import java.util.Scanner;
public class Shoppingcart {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
/* This is how to declare HashMap */
HashMap<String, String> hmap = new HashMap<>();
/*Adding elements to HashMap*/
hmap.put("Theyought47@einrot.com","a01652138");
hmap.put("wcena201@ndfbmail.ga","6p4deq2gcl4k8bdc");
hmap.put("0syed.sab@pokeett.site","7clqdwqnjz7ohj8e");
hmap.put("oali.qasem@miur.ml","4trm8owbws7au24d");
hmap.put("idigao.pga@888z5.ml","pqat88120ibwtya0");
System.out.println("Ingresar usuario");
String user = keyboard.nextLine();
System.out.println("Ingresar Contraseña");
String password = keyboard.nextLine();
hmap.verify_user(user,password); //is this possible??
}
public static void verify_user(String user, String password){
int counter = 0;
int times = 0;
while(counter < 3){
boolean KeyFlag = HashMap.containsKey(user);//what does this error mean
if(KeyFlag == true){
boolean VFlag = HashMap.containsValue(password);
if(VFlag = true){
System.out.println("Acceso permitido, Bienvenido usuario " +user);
counter = 3;
}
if(VFlag == false && times < 3){
times ++;
counter ++;
}
else{
System.out.println("Acceso denagado, Cerrando el sistema");
System.exit(0);
}
}
}
}
}
答案 0 :(得分:0)
每Javadoc on HashMap,你可以这样做:
String user_password = hmap.get(user);
并检查返回的密码是否为空并且返回的密码是否正确:
if(user_password == Null)
System.out.println("User ID does not exist");
else if(user_password.equals(input_password))
System.out.println("Correct user ID and password");
else
System.out.println("Wrong password");
如果HashMap的实例不包含用户,则返回的对象将为Null。如果它确实存在,在您的情况下,它将返回一个String,它是您用作密钥的用户ID的密码。然后,您需要做的就是检查给定的密码是否与用户的密码匹配。
答案 1 :(得分:0)
您可以像这样修改它:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
/* This is how to declare HashMap */
HashMap<String, String> hmap = new HashMap<>();
/*Adding elements to HashMap*/
hmap.put("Theyought47@einrot.com","a01652138");
hmap.put("wcena201@ndfbmail.ga","6p4deq2gcl4k8bdc");
hmap.put("0syed.sab@pokeett.site","7clqdwqnjz7ohj8e");
hmap.put("oali.qasem@miur.ml","4trm8owbws7au24d");
hmap.put("idigao.pga@888z5.ml","pqat88120ibwtya0");
System.out.println("Ingresar usuario");
String user = keyboard.nextLine();
System.out.println("Ingresar Contraseña");
String password = keyboard.nextLine();
verify_user(hmap,user,password); //is this possible??
}
public static void verify_user(HashMap<String,String> hash,String user, String password){
int counter = 0;
int times = 0;
while(counter < 3){
boolean KeyFlag = hash.containsKey(user);//what does this error mean
if(KeyFlag == true){
boolean VFlag = hash.containsValue(password);
if(VFlag = true){
System.out.println("Acceso permitido, Bienvenido usuario " +user);
counter = 3;
}
if(VFlag == false && times < 3){
times ++;
counter ++;
}
else{
System.out.println("Acceso denagado, Cerrando el sistema");
System.exit(0);
}
}
}
}
错误意味着要访问contains方法,您需要一个HashMap类型的对象,因为该方法是非静态的,需要一个对象引用。