在Java中打印特定的阵列位置(控制台)

时间:2017-05-20 12:03:22

标签: java arrays netbeans printing console

我有一个Java数组(基于控制台),我想打印出用户输入时存储在其中一个位置的详细信息。例如,他们在控制台应用程序中输入“3”,然后程序以与请求匹配的详细信息进行响应。

提前致谢!

if(input.matches("S")){
// If user input matches with S, then the user willSSN be asked to enter a array position and then the corresponding information will be shown



    KeyStroke[] patientsDetails = new KeyStroke[5];
    patientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489);
    patientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587);
    patientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842);
    patientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340);
    patientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098);

    System.out.println("Enter an array postion from 0 to 4 to show paitients corresponding postion and details");

    number1 = enterNumber0.nextInt();
     if (input.matches("0")){

        System.out.println(patientsDetails[0]);
    }

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,问题就在您将"Enter an array position from 0 to 4..."打印到控制台后。

尝试并替换

number1 = enterNumber0.nextInt();
     if (input.matches("0")){

        System.out.println(patientsDetails[0]);
    }

number1 = enterNumber0.nextInt(); // is enterNumber0 the name of your Scanner object?
System.out.println(patientsDetails[number1]); // I assume that number1 is declared as an `int` somewhere in your code?

如果我没有全面了解您的问题,请原谅我并尝试更好地澄清问题。