简体   繁体   English

Java HashMap 和 Iterator - 如何知道值是否与用户相同

[英]Java HashMap and Iterator - How to know if value is the same as user

I'm learning how to use a hashmap.我正在学习如何使用哈希图。 I am trying to build an ATM program which allow the user to log in if they enter their card number and pin number correctly.我正在尝试构建一个 ATM 程序,如果用户正确输入他们的卡号和密码,则允许用户登录。 Below is my code.下面是我的代码。

import java.util.*;
import java.util.Scanner;
import java.util.Iterator;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Map<String, String> map = new HashMap<>();
        map.put("123456789", "123456");
        map.put("987654321", "654321");

        Scanner in = new Scanner(System.in);
        System.out.println("Enter Your Card Number");
        String card = in.next();
        System.out.println("Enter Your Pin Number");
        String pin = in.next();
        String everything = card  + "=" + pin;

        Iterator i = map.entrySet().iterator();
        while(i.hasNext()) {
            String s = (String) i.next();
            if(everything == i.next()) {
                System.out.println("Congratulations for logging in");
            } else {
                System.out.println(i.next());
                System.out.println("Wrong Card Number Or Pin");
                System.out.println("Enter Your Card Number");
                card = in.next();
                System.out.println("Enter Your Pin Number");
                pin = in.next();
                everything = card + "=" + pin;
            }
        }

    }
}

My question is how do I check is the user input is the same as one of the values I have entered in my map?我的问题是如何检查用户输入是否与我在地图中输入的值之一相同? Thank you.谢谢你。

Map#get

As a rule of thumb, if you're iterating through a map looking for a particular entry, you're doing it wrong.根据经验,如果您正在遍历地图以查找特定条目,那么您做错了。 The whole point of a map is that you can look up a key directly.地图的全部意义在于您可以直接查找密钥。 Call Map#get , pass the key, get back the value mapped to that key.调用Map#get ,传递键,取回映射到该键的值。

I assume the Map maps from card number to PIN.我假设地图从卡号映射到 PIN。 On that basis:在此基础上:

boolean valid = false;
Scanner in = new Scanner(System.in);
while (!valid) {
    System.out.println("Enter Your Card Number");
    String card = in.next();
    System.out.println("Enter Your Pin Number");
    String pin = in.next();
    String actualPin = map.get(card);
    valid = pin.equals(actualPin);
    if (!valid) {
        // card number not found
        // or pins do not match
        System.out.println("Try again");
    }
}

By the way, you should add more code to check for null .顺便说一句,您应该添加更多代码来检查null If the user submits a card number not currently found in our Map , a null is returned.如果用户提交的卡号当前在我们的Map中找不到,则返回null

I think maybe you're over thinking this a little.我想你可能有点想多了。 ALl you need to do is simply check firstly if the card number (key) is in the hashmap and if so if the pin matches.您需要做的只是首先检查卡号(密钥)是否在哈希图中,如果是,则密码是否匹配。

if (map.containsKey(card) && map.get(card).equals(pin)) {
    System.out.println("Congratulations for logging in");
} else {
    System.out.println("I'm sorry your card number or pin was incorrect");
}

That should work for you, I recomend you read the documentation on HashMaps to get a better feel for them: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html这应该对您有用,我建议您阅读有关 HashMaps 的文档以更好地了解它们: https ://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM