Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关于如何构建使用军事身份验证表的程序的几个问题?_Java_Arrays_Arraylist - Fatal编程技术网

Java 关于如何构建使用军事身份验证表的程序的几个问题?

Java 关于如何构建使用军事身份验证表的程序的几个问题?,java,arrays,arraylist,Java,Arrays,Arraylist,您将使用什么数据结构数组来存储身份验证表,并让用户使用扫描仪输入从表中获取特定数据。例如,如果用户输入delta-three,程序应该用romeo-three响应并验证它 问题: 我应该使用什么数据结构?也许是ArrayList 如何为数据结构提供身份验证表的维度?我假设我可以使用索引并输入一个数字来表示行和列 如何使填充表格的对象没有重复项,否则需要重新生成 如何生成新表 那么,我需要什么样的Java导入,Java/Netbeans中是否已经有内置函数可以帮助我 是的,这似乎是很多,但我与这类

您将使用什么数据结构数组来存储身份验证表,并让用户使用扫描仪输入从表中获取特定数据。例如,如果用户输入delta-three,程序应该用romeo-three响应并验证它

问题:

我应该使用什么数据结构?也许是ArrayList

如何为数据结构提供身份验证表的维度?我假设我可以使用索引并输入一个数字来表示行和列

如何使填充表格的对象没有重复项,否则需要重新生成

如何生成新表

那么,我需要什么样的Java导入,Java/Netbeans中是否已经有内置函数可以帮助我

是的,这似乎是很多,但我与这类事情斗争,需要专家的帮助

如果您需要身份验证表的示例。以下是一个例子:

public class AuthenticationTable 
{ //Start of class method.

    public static void main(String[] args) 
    { //Start of main method
        String [] [] Table = {{"E3","H5","R9","W8","S1"},
                              {"Z9","P5","K8","X5","I9"},
                              {"B2","F7","L2","M4","H3"},
                              {"J7","A7","N2","R6","V4"},
                              {"O9","W3","E8","U4","E8"}};

        System.out.println(Table[2][1]);
        System.out.println(Table[4][0]);

    } //End of main method
}//End of class method

这个问题听起来很像家庭作业,因此我将尝试简单地为您指出正确的方向:

请看下面的图片。您可以将用户提供的内容存储为密钥,并将相应的身份验证存储为值

HashMap是一个键值结构,因此您可以将请求预期的响应存储为键值结构

HashMap结构的键必须是唯一的。因此,您将不会有任何副本。您还可以使用containsKey方法查看密钥是否已经存在

以你为例,

例如,如果用户输入delta 3,程序应该响应 和罗密欧四号在一起,并验证它

你可以这样做:

Map<String, String> authTable = new HashMap<String, String>();
authTable.put("D3", "R4");
...

String userInput = "D3";
System.out.println(authTable.get(userInput));   //This should yield R4, assuming that it exists.

感谢@npinti的回复。那么,如何创建或使用hashmap呢?我以前从未使用过。这很简单吗?军方认为这是身份验证表的一个例子,因为他们使用它来确保该地区在战斗中是清晰的。我还需要创建一个包含getter、setter和objects细节的java类吗?@JacobFellows:是的,我理解你的观点,这就是为什么我把它作为旁注。就我个人而言,我会创建一个新类,称之为AuthenticationProvider或类似的类,它提供了一个方法来验证您所给出的调用,而不公开哈希映射本身。这将使您了解两个原则:单一责任和封装。如果您想更进一步,您还可以看看如何使您的AuthenticationProvider成为单例,这将使您暴露于一些设计模式,但这可能超出此任务。因此,如果我创建一个类并将其链接到主类,它应该允许我保存某些详细信息。此外,我还尝试在Netbeans中制作了一个您在上面给出的示例的版本,这就是我到目前为止所做的public static void mainString[]args{Scanner scan=new ScannerSystem.in;Map authTable=new HashMap;authTable.putD3,R4;System.out.print之后的坐标:;scan.next;System.out.printauthTable.removescan;}代码允许用户输入文本,但不返回变量R4。所以我要为getter创建一个类。@JacobFellows:从技术上讲,如果您将hash映射与您的类解耦,并将其放在另一个类中,那么更改获取数据的方式会更容易。我将很快更新我的答案,以提供一个快速的工作示例。
public class AuthenticationProvider {
    private Map<String, String> authTable;

    public AuthenticationProvider()
    {
        this.authTable = new HashMap<>();
    }

    public String authenticate(String input)
    {
        return this.authTable.get(input);
    }

    public void addAuthentication(String source, String expected)
    {
        if(!this.authTable.containsKey(source))
            this.authTable.put(source, expected);
    }
}
    Scanner scan = new Scanner(System.in);
    AuthenticationProvider authProvider = new AuthenticationProvider();
    authProvider.addAuthentication("D3", "R4");

    System.out.print("The co-ordinates that your after: ");
    String userInput = scan.nextLine();
    System.out.print(authProvider.authenticate(userInput));